gpm/cmd/add.go

74 lines
1.4 KiB
Go
Raw Normal View History

package cmd
import (
"regexp"
"strings"
"go.jolheiser.com/gpm/config"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Add = cli.Command{
Name: "add",
Usage: "Add a package",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Overwrite existing package without prompt",
},
},
Action: doAdd,
}
var vPattern = regexp.MustCompile(`v\d+$`)
func doAdd(ctx *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
goGetQuestion := &survey.Input{
Message: "Package go-get import",
}
var goGetAnswer string
if err := survey.AskOne(goGetQuestion, &goGetAnswer); err != nil {
return err
}
goGet := strings.Split(goGetAnswer, "/")
defaultName := goGet[len(goGet)-1]
// Check if go-get is actually pointing to a versioned module
if vPattern.MatchString(defaultName) {
defaultName = goGet[len(goGet)-2]
}
nameQuestion := &survey.Input{
Message: "Package name for gpm",
Default: defaultName,
}
var nameAnswer string
if err := survey.AskOne(nameQuestion, &nameAnswer); err != nil {
return err
}
pkg := config.Package{
Name: nameAnswer,
Import: goGetAnswer,
}
cfg.AddPackages(ctx.Bool("force"), pkg)
if err := cfg.Save(); err != nil {
return err
}
beaver.Infof("Added `%s` to local gpm.", nameAnswer)
return nil
}