2020-02-19 05:18:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-02-19 05:39:54 +00:00
|
|
|
"gitea.com/gpm/gpm/modules/config"
|
2020-02-19 05:18:36 +00:00
|
|
|
"gitea.com/jolheiser/beaver"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-19 16:48:55 +00:00
|
|
|
"regexp"
|
2020-02-19 05:18:36 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
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-02-19 16:48:55 +00:00
|
|
|
config.AddPackages(ctx.Bool("force"), pkg)
|
2020-02-19 05:18:36 +00:00
|
|
|
|
|
|
|
if err := config.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
beaver.Infof("Added `%s` to local gpm.", nameAnswer)
|
|
|
|
return nil
|
|
|
|
}
|