gpm/internal/cli/remove.go

68 lines
1.2 KiB
Go
Raw Normal View History

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 Remove = cli.Command{
Name: "remove",
Aliases: []string{"rm"},
Usage: "Remove package(s)",
Before: localOrToken,
Action: doRemove,
}
func doRemove(_ *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 remove",
Options: pkgSlice,
}
var pkgName string
if err := survey.AskOne(pkgQuestion, &pkgName); err != nil {
return err
}
pkg := gpm.Package{
Name: pkgName,
Import: pkgMap[pkgName].Import,
}
if localFlag {
db, err := database.Load(databaseFlag)
if err != nil {
return err
}
if err := db.RemovePackage(pkg.Name); err != nil {
return err
}
} else {
client := gpm.New(tokenFlag, gpm.WithServer(serverFlag))
if err := client.Remove(context.Background(), pkg); err != nil {
return err
}
}
log.Info().Msgf("Removed %q", pkgName)
return nil
}