Change remove command

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/7/head
jolheiser 2021-02-27 22:44:22 -06:00
parent 472ba97d1e
commit cb0dd34973
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 41 additions and 21 deletions

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"context" "context"
"go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/cmd/flags" "go.jolheiser.com/gpm/cmd/flags"
"go.jolheiser.com/gpm/database" "go.jolheiser.com/gpm/database"
@ -9,7 +10,6 @@ import (
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
) )
var Remove = cli.Command{ var Remove = cli.Command{
@ -21,30 +21,50 @@ var Remove = cli.Command{
} }
func doRemove(_ *cli.Context) error { func doRemove(_ *cli.Context) error {
pkgQuestion := &survey.Input{ pkgs, err := listPackages()
Message: "Package name", if err != nil {
}
var pkgAnswer string
if err := survey.AskOne(pkgQuestion, &pkgAnswer); err != nil {
return err return err
} }
if flags.Local { pkgSlice := make([]string, len(pkgs))
db, err := database.Load(flags.Database) pkgMap := make(map[string]gpm.Package)
if err != nil { for idx, pkg := range pkgs {
return err pkgSlice[idx] = pkg.Name
} pkgMap[pkg.Name] = pkg
if err := db.RemovePackage(pkgAnswer); err != nil { }
return err
} pkgQuestion := &survey.MultiSelect{
} else { Message: "Select package to remove",
client := gpm.New(flags.Token, gpm.WithServer(flags.Server)) Options: pkgSlice,
if err := client.Remove(context.Background(), gpm.Package{Name: pkgAnswer}); err != nil { }
return err
} var pkgNames []string
if err := survey.AskOne(pkgQuestion, &pkgNames); err != nil {
return err
}
for _, pkgName := range pkgNames {
pkg := gpm.Package{
Name: pkgName,
Import: pkgMap[pkgName].Import,
}
if flags.Local {
db, err := database.Load(flags.Database)
if err != nil {
return err
}
if err := db.RemovePackage(pkg.Name); err != nil {
return err
}
} else {
client := gpm.New(flags.Token, gpm.WithServer(flags.Server))
if err := client.Remove(context.Background(), pkg); err != nil {
return err
}
}
beaver.Infof("Removed %s", yellow.Format(pkgName))
} }
beaver.Infof("Removed %s", yellow.Format(pkgAnswer))
return nil return nil
} }