90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
|
package cli
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
|
||
|
"go.jolheiser.com/gpm"
|
||
|
"go.jolheiser.com/gpm/internal/database"
|
||
|
|
||
|
"github.com/AlecAivazis/survey/v2"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
)
|
||
|
|
||
|
var Add = cli.Command{
|
||
|
Name: "add",
|
||
|
Aliases: []string{"a"},
|
||
|
Usage: "Add a package",
|
||
|
Flags: []cli.Flag{
|
||
|
&cli.BoolFlag{
|
||
|
Name: "force",
|
||
|
Aliases: []string{"f"},
|
||
|
Usage: "Overwrite existing package without prompt",
|
||
|
Destination: &forceFlag,
|
||
|
},
|
||
|
&cli.BoolFlag{
|
||
|
Name: "local",
|
||
|
Aliases: []string{"l"},
|
||
|
Usage: "local mode",
|
||
|
Destination: &localFlag,
|
||
|
},
|
||
|
},
|
||
|
Before: localOrToken,
|
||
|
Action: doAdd,
|
||
|
}
|
||
|
|
||
|
var vPattern = regexp.MustCompile(`v\d+$`)
|
||
|
|
||
|
func doAdd(_ *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 := gpm.Package{
|
||
|
Name: nameAnswer,
|
||
|
Import: goGetAnswer,
|
||
|
}
|
||
|
|
||
|
if localFlag {
|
||
|
db, err := database.Load(databaseFlag)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := db.PutPackage(pkg); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
client := gpm.New(tokenFlag, gpm.WithServer(serverFlag))
|
||
|
if err := client.Add(context.Background(), pkg); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
log.Info().Msgf("Added %q", nameAnswer)
|
||
|
return nil
|
||
|
}
|