2020-02-19 05:18:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-02-28 04:20:14 +00:00
|
|
|
"context"
|
2020-10-07 04:11:36 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
"go.jolheiser.com/gpm/cmd/flags"
|
|
|
|
"go.jolheiser.com/gpm/database"
|
|
|
|
"go.jolheiser.com/gpm/go-gpm"
|
2020-10-07 04:11:36 +00:00
|
|
|
|
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{
|
2021-02-28 04:20:14 +00:00
|
|
|
Name: "force",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Usage: "Overwrite existing package without prompt",
|
|
|
|
Destination: &flags.Force,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "local",
|
|
|
|
Aliases: []string{"l"},
|
|
|
|
Usage: "local mode",
|
|
|
|
Destination: &flags.Local,
|
2020-02-19 05:18:36 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-28 04:20:14 +00:00
|
|
|
Before: localOrToken,
|
2020-02-19 05:18:36 +00:00
|
|
|
Action: doAdd,
|
|
|
|
}
|
|
|
|
|
2020-02-19 16:48:55 +00:00
|
|
|
var vPattern = regexp.MustCompile(`v\d+$`)
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
func doAdd(_ *cli.Context) error {
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
pkg := gpm.Package{
|
2020-02-19 05:39:54 +00:00
|
|
|
Name: nameAnswer,
|
|
|
|
Import: goGetAnswer,
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
if flags.Local {
|
|
|
|
db, err := database.Load(flags.Database)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.PutPackage(pkg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
client := gpm.New(flags.Token, gpm.WithServer(flags.Server))
|
|
|
|
if err := client.Add(context.Background(), pkg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 04:20:14 +00:00
|
|
|
beaver.Infof("Added %s", yellow.Format(nameAnswer))
|
2020-02-19 05:18:36 +00:00
|
|
|
return nil
|
|
|
|
}
|