package cmd import ( "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" "go.jolheiser.com/beaver" "go.jolheiser.com/gpm/modules/config" "regexp" "strings" ) 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 { 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, } config.AddPackages(ctx.Bool("force"), pkg) if err := config.Save(); err != nil { return err } beaver.Infof("Added `%s` to local gpm.", nameAnswer) return nil }