2020-02-19 05:18:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-07 04:11:36 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.jolheiser.com/gpm/config"
|
|
|
|
|
2020-02-19 05:18:36 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-20 19:09:56 +00:00
|
|
|
"go.jolheiser.com/beaver"
|
2020-02-19 05:18:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Add = cli.Command{
|
|
|
|
Name: "add",
|
2020-02-19 16:48:55 +00:00
|
|
|
Usage: "Add a package",
|
2020-02-19 05:18:36 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Aliases: []string{"f"},
|
2020-02-19 16:48:55 +00:00
|
|
|
Usage: "Overwrite existing package without prompt",
|
2020-02-19 05:18:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: doAdd,
|
|
|
|
}
|
|
|
|
|
2020-02-19 16:48:55 +00:00
|
|
|
var vPattern = regexp.MustCompile(`v\d+$`)
|
|
|
|
|
2020-02-19 05:18:36 +00:00
|
|
|
func doAdd(ctx *cli.Context) error {
|
2020-10-07 04:11:36 +00:00
|
|
|
cfg, err := config.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-19 05:18:36 +00:00
|
|
|
|
|
|
|
goGetQuestion := &survey.Input{
|
2020-02-19 05:39:54 +00:00
|
|
|
Message: "Package go-get import",
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
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
|
2020-02-19 16:48:55 +00:00
|
|
|
if vPattern.MatchString(defaultName) {
|
2020-02-19 05:18:36 +00:00
|
|
|
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{
|
2020-02-19 05:39:54 +00:00
|
|
|
Name: nameAnswer,
|
|
|
|
Import: goGetAnswer,
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
2020-10-07 04:11:36 +00:00
|
|
|
cfg.AddPackages(ctx.Bool("force"), pkg)
|
2020-02-19 05:18:36 +00:00
|
|
|
|
2020-10-07 04:11:36 +00:00
|
|
|
if err := cfg.Save(); err != nil {
|
2020-02-19 05:18:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
beaver.Infof("Added `%s` to local gpm.", nameAnswer)
|
|
|
|
return nil
|
|
|
|
}
|