Add import and list commands, cleanup

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/1/head
jolheiser 2020-02-19 10:48:55 -06:00
parent 62ad64516c
commit afe09ba524
No known key found for this signature in database
GPG Key ID: 83E486E71AFEB820
10 changed files with 153 additions and 36 deletions

29
README.md 100644
View File

@ -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.

View File

@ -1,27 +1,29 @@
package cmd
import (
"fmt"
"gitea.com/gpm/gpm/modules/config"
"gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"regexp"
"strings"
)
var Add = cli.Command{
Name: "add",
Usage: "Add a gpm package",
Usage: "Add a package",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Overwrite if the package already exists",
Usage: "Overwrite existing package without prompt",
},
},
Action: doAdd,
}
var vPattern = regexp.MustCompile(`v\d+$`)
func doAdd(ctx *cli.Context) error {
goGetQuestion := &survey.Input{
@ -36,7 +38,7 @@ func doAdd(ctx *cli.Context) error {
goGet := strings.Split(goGetAnswer, "/")
defaultName := goGet[len(goGet)-1]
// 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]
}
@ -54,31 +56,7 @@ func doAdd(ctx *cli.Context) error {
Name: nameAnswer,
Import: goGetAnswer,
}
if !ctx.Bool("force") {
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)
}
config.AddPackages(ctx.Bool("force"), pkg)
if err := config.Save(); err != nil {
return err

View File

@ -10,6 +10,7 @@ import (
var Config = cli.Command{
Name: "config",
Aliases: []string{"cfg"},
Usage: "Configure local gpm",
Action: doConfig,
}

View File

@ -8,7 +8,7 @@ import (
var Export = cli.Command{
Name: "export",
Usage: "Export local gpm packages",
Usage: "Export JSON for local packages",
Action: doExport,
}

View File

@ -16,7 +16,7 @@ import (
var Get = cli.Command{
Name: "get",
Usage: "Get gpm package(s)",
Usage: "Get package(s)",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "ignore-local",

52
cmd/import.go 100644
View File

@ -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
}

21
cmd/list.go 100644
View File

@ -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
}

View File

@ -11,7 +11,8 @@ import (
var Remove = cli.Command{
Name: "remove",
Usage: "Remove a gpm package",
Aliases: []string{"rm"},
Usage: "Remove a package",
Action: doRemove,
}

View File

@ -20,7 +20,9 @@ func main() {
app.Commands = []*cli.Command{
&cmd.Add,
&cmd.Remove,
&cmd.List,
&cmd.Get,
&cmd.Import,
&cmd.Export,
&cmd.Config,
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"os"
@ -13,7 +14,7 @@ import (
var (
configPath string
cfg *config
cfg *Config
// Config items
@ -21,7 +22,7 @@ var (
Packages []Package
)
type config struct {
type Config struct {
GPMURL string `toml:"gpm-url" json:"gpm_url"`
Packages []Package `toml:"package" json:"packages"`
}
@ -95,3 +96,35 @@ func PackageMap() map[string]string {
}
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
}
}
}
}