86 lines
1.6 KiB
Go
86 lines
1.6 KiB
Go
|
package cli
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"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 Update = cli.Command{
|
||
|
Name: "update",
|
||
|
Aliases: []string{"u"},
|
||
|
Usage: "Update a package",
|
||
|
Flags: []cli.Flag{
|
||
|
&cli.BoolFlag{
|
||
|
Name: "local",
|
||
|
Aliases: []string{"l"},
|
||
|
Usage: "local mode",
|
||
|
Destination: &localFlag,
|
||
|
},
|
||
|
},
|
||
|
Before: localOrToken,
|
||
|
Action: doUpdate,
|
||
|
}
|
||
|
|
||
|
func doUpdate(_ *cli.Context) error {
|
||
|
pkgs, err := listPackages()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
pkgSlice := make([]string, len(pkgs))
|
||
|
pkgMap := make(map[string]gpm.Package)
|
||
|
for idx, pkg := range pkgs {
|
||
|
pkgSlice[idx] = pkg.Name
|
||
|
pkgMap[pkg.Name] = pkg
|
||
|
}
|
||
|
|
||
|
pkgQuestion := &survey.Select{
|
||
|
Message: "Select package to update",
|
||
|
Options: pkgSlice,
|
||
|
}
|
||
|
|
||
|
var pkgName string
|
||
|
if err := survey.AskOne(pkgQuestion, &pkgName); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
importQuestion := &survey.Input{
|
||
|
Message: "New import path",
|
||
|
Default: pkgMap[pkgName].Import,
|
||
|
}
|
||
|
|
||
|
var importPath string
|
||
|
if err := survey.AskOne(importQuestion, &importPath); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
pkg := gpm.Package{
|
||
|
Name: pkgName,
|
||
|
Import: importPath,
|
||
|
}
|
||
|
|
||
|
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.Update(context.Background(), pkg); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
log.Info().Msgf("Updated %q", pkgName)
|
||
|
return nil
|
||
|
}
|