Add import and list commands, cleanup
Signed-off-by: jolheiser <john.olheiser@gmail.com>pull/1/head
parent
62ad64516c
commit
afe09ba524
|
@ -0,0 +1,29 @@
|
||||||
|
# GPM
|
||||||
|
|
||||||
|
Go Package Mapping
|
||||||
|
|
||||||
|
### Motivation
|
||||||
|
|
||||||
|
I personally use a lot of nice libraries in Go, but every time I start a new project I have to go hunting for import paths again!
|
||||||
|
|
||||||
|
Enter GPM, a glorified mapping of simple names to go-get imports.
|
||||||
|
|
||||||
|
For example, I use [urfave/cli](https://github.com/urfave/cli) for all of my CLI projects. I've used it enough times to remember the import path, but let's say I didn't.
|
||||||
|
Using either a GPM server or local config, I can instead `gpm get cli` which finds `cli` in my map and runs `go get github.com/urfave/cli/v2`.
|
||||||
|
|
||||||
|
### Commands
|
||||||
|
|
||||||
|
* `add` - Add a local package
|
||||||
|
* `remove` - Remove a local package
|
||||||
|
* `list` - List local packages
|
||||||
|
* `config` - Change local configuration (GPM server)
|
||||||
|
* `export` - Export local packages to JSON
|
||||||
|
* `import` - Import JSON to local packages
|
||||||
|
|
||||||
|
|
||||||
|
### Server
|
||||||
|
|
||||||
|
If GPM doesn't find a package locally, it can call out to a configurable [gpm server](https://gitea.com/jolheiser/gpm-server) to find a package there.
|
||||||
|
This makes it much simpler to have a central library of packages rather than exporting and importing between environments.
|
||||||
|
|
||||||
|
The `import` and `export` commands should work between the CLI and server for easy transitions.
|
36
cmd/add.go
36
cmd/add.go
|
@ -1,27 +1,29 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"gitea.com/gpm/gpm/modules/config"
|
"gitea.com/gpm/gpm/modules/config"
|
||||||
"gitea.com/jolheiser/beaver"
|
"gitea.com/jolheiser/beaver"
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Add = cli.Command{
|
var Add = cli.Command{
|
||||||
Name: "add",
|
Name: "add",
|
||||||
Usage: "Add a gpm package",
|
Usage: "Add a package",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "force",
|
Name: "force",
|
||||||
Aliases: []string{"f"},
|
Aliases: []string{"f"},
|
||||||
Usage: "Overwrite if the package already exists",
|
Usage: "Overwrite existing package without prompt",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: doAdd,
|
Action: doAdd,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var vPattern = regexp.MustCompile(`v\d+$`)
|
||||||
|
|
||||||
func doAdd(ctx *cli.Context) error {
|
func doAdd(ctx *cli.Context) error {
|
||||||
|
|
||||||
goGetQuestion := &survey.Input{
|
goGetQuestion := &survey.Input{
|
||||||
|
@ -36,7 +38,7 @@ func doAdd(ctx *cli.Context) error {
|
||||||
goGet := strings.Split(goGetAnswer, "/")
|
goGet := strings.Split(goGetAnswer, "/")
|
||||||
defaultName := goGet[len(goGet)-1]
|
defaultName := goGet[len(goGet)-1]
|
||||||
// Check if go-get is actually pointing to a versioned module
|
// Check if go-get is actually pointing to a versioned module
|
||||||
if strings.HasPrefix(defaultName, "v") {
|
if vPattern.MatchString(defaultName) {
|
||||||
defaultName = goGet[len(goGet)-2]
|
defaultName = goGet[len(goGet)-2]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,31 +56,7 @@ func doAdd(ctx *cli.Context) error {
|
||||||
Name: nameAnswer,
|
Name: nameAnswer,
|
||||||
Import: goGetAnswer,
|
Import: goGetAnswer,
|
||||||
}
|
}
|
||||||
if !ctx.Bool("force") {
|
config.AddPackages(ctx.Bool("force"), pkg)
|
||||||
for idx, p := range config.Packages {
|
|
||||||
if strings.EqualFold(p.Name, nameAnswer) {
|
|
||||||
forceQuestion := &survey.Confirm{
|
|
||||||
Message: "This package already exists, do you want to overwrite?",
|
|
||||||
Default: false,
|
|
||||||
}
|
|
||||||
var force bool
|
|
||||||
|
|
||||||
if err := survey.AskOne(forceQuestion, &force); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !force {
|
|
||||||
return fmt.Errorf("leaving package `%s` as-is", nameAnswer)
|
|
||||||
}
|
|
||||||
|
|
||||||
config.Packages[idx] = pkg
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config.Packages = append(config.Packages, pkg)
|
|
||||||
} else {
|
|
||||||
config.Packages = append(config.Packages, pkg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := config.Save(); err != nil {
|
if err := config.Save(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
var Config = cli.Command{
|
var Config = cli.Command{
|
||||||
Name: "config",
|
Name: "config",
|
||||||
Aliases: []string{"cfg"},
|
Aliases: []string{"cfg"},
|
||||||
|
Usage: "Configure local gpm",
|
||||||
Action: doConfig,
|
Action: doConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
var Export = cli.Command{
|
var Export = cli.Command{
|
||||||
Name: "export",
|
Name: "export",
|
||||||
Usage: "Export local gpm packages",
|
Usage: "Export JSON for local packages",
|
||||||
Action: doExport,
|
Action: doExport,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
|
|
||||||
var Get = cli.Command{
|
var Get = cli.Command{
|
||||||
Name: "get",
|
Name: "get",
|
||||||
Usage: "Get gpm package(s)",
|
Usage: "Get package(s)",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "ignore-local",
|
Name: "ignore-local",
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"gitea.com/gpm/gpm/modules/config"
|
||||||
|
"gitea.com/jolheiser/beaver"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Import = cli.Command{
|
||||||
|
Name: "import",
|
||||||
|
Usage: "Import JSON for local packages",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "force",
|
||||||
|
Aliases: []string{"f"},
|
||||||
|
Usage: "Overwrite any existing packages without prompt",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: doImport,
|
||||||
|
}
|
||||||
|
|
||||||
|
func doImport(ctx *cli.Context) error {
|
||||||
|
files := ctx.Args().Slice()
|
||||||
|
if len(files) == 0 {
|
||||||
|
return errors.New("must provide a list of JSON files to import from")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
body, err := ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
beaver.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfg config.Config
|
||||||
|
if err := json.Unmarshal(body, &cfg); err != nil {
|
||||||
|
beaver.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
config.AddPackages(ctx.Bool("force"), cfg.Packages...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := config.Save(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
beaver.Info("Import complete")
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gitea.com/gpm/gpm/modules/config"
|
||||||
|
"gitea.com/jolheiser/beaver"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var List = cli.Command{
|
||||||
|
Name: "list",
|
||||||
|
Aliases: []string{"l"},
|
||||||
|
Usage: "List local packages",
|
||||||
|
Action: doList,
|
||||||
|
}
|
||||||
|
|
||||||
|
func doList(ctx *cli.Context) error {
|
||||||
|
for _, pkg := range config.Packages {
|
||||||
|
beaver.Infof("%s -> %s", pkg.Name, pkg.Import)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -11,7 +11,8 @@ import (
|
||||||
|
|
||||||
var Remove = cli.Command{
|
var Remove = cli.Command{
|
||||||
Name: "remove",
|
Name: "remove",
|
||||||
Usage: "Remove a gpm package",
|
Aliases: []string{"rm"},
|
||||||
|
Usage: "Remove a package",
|
||||||
Action: doRemove,
|
Action: doRemove,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
main.go
2
main.go
|
@ -20,7 +20,9 @@ func main() {
|
||||||
app.Commands = []*cli.Command{
|
app.Commands = []*cli.Command{
|
||||||
&cmd.Add,
|
&cmd.Add,
|
||||||
&cmd.Remove,
|
&cmd.Remove,
|
||||||
|
&cmd.List,
|
||||||
&cmd.Get,
|
&cmd.Get,
|
||||||
|
&cmd.Import,
|
||||||
&cmd.Export,
|
&cmd.Export,
|
||||||
&cmd.Config,
|
&cmd.Config,
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.com/jolheiser/beaver"
|
"gitea.com/jolheiser/beaver"
|
||||||
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,7 +14,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configPath string
|
configPath string
|
||||||
cfg *config
|
cfg *Config
|
||||||
|
|
||||||
// Config items
|
// Config items
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ var (
|
||||||
Packages []Package
|
Packages []Package
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type Config struct {
|
||||||
GPMURL string `toml:"gpm-url" json:"gpm_url"`
|
GPMURL string `toml:"gpm-url" json:"gpm_url"`
|
||||||
Packages []Package `toml:"package" json:"packages"`
|
Packages []Package `toml:"package" json:"packages"`
|
||||||
}
|
}
|
||||||
|
@ -95,3 +96,35 @@ func PackageMap() map[string]string {
|
||||||
}
|
}
|
||||||
return pkgs
|
return pkgs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddPackages(force bool, pkgs ...Package) {
|
||||||
|
for _, pkg := range pkgs {
|
||||||
|
for idx, p := range Packages {
|
||||||
|
if strings.EqualFold(p.Name, pkg.Name) {
|
||||||
|
if force {
|
||||||
|
Packages[idx] = pkg
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
forceQuestion := &survey.Confirm{
|
||||||
|
Message: fmt.Sprintf("Package `%s` (%s) already exists. Overwrite with `%s`?", p.Name, p.Import, p.Import),
|
||||||
|
Default: false,
|
||||||
|
}
|
||||||
|
var forceAnswer bool
|
||||||
|
|
||||||
|
if err := survey.AskOne(forceQuestion, &forceAnswer); err != nil {
|
||||||
|
beaver.Error(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !forceAnswer {
|
||||||
|
beaver.Errorf("leaving package `%s` as-is", pkg.Name)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
Packages[idx] = pkg
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue