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 (
"context"
"go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/cmd/flags"
"go.jolheiser.com/gpm/database"
@ -9,7 +10,6 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Remove = cli.Command{
@ -21,30 +21,50 @@ var Remove = cli.Command{
}
func doRemove(_ *cli.Context) error {
pkgQuestion := &survey.Input{
Message: "Package name",
}
var pkgAnswer string
if err := survey.AskOne(pkgQuestion, &pkgAnswer); err != nil {
pkgs, err := listPackages()
if err != nil {
return err
}
if flags.Local {
db, err := database.Load(flags.Database)
if err != nil {
return err
}
if err := db.RemovePackage(pkgAnswer); err != nil {
return err
}
} else {
client := gpm.New(flags.Token, gpm.WithServer(flags.Server))
if err := client.Remove(context.Background(), gpm.Package{Name: pkgAnswer}); 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.MultiSelect{
Message: "Select package to remove",
Options: pkgSlice,
}
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
}