2020-02-19 05:18:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-02-28 05:04:05 +00:00
|
|
|
"context"
|
2020-10-07 04:11:36 +00:00
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
"go.jolheiser.com/gpm/cmd/flags"
|
|
|
|
"go.jolheiser.com/gpm/database"
|
|
|
|
"go.jolheiser.com/gpm/go-gpm"
|
2020-10-07 04:11:36 +00:00
|
|
|
|
2020-02-19 05:18:36 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-20 19:09:56 +00:00
|
|
|
"go.jolheiser.com/beaver"
|
2020-02-19 05:18:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Remove = cli.Command{
|
2020-02-19 16:48:55 +00:00
|
|
|
Name: "remove",
|
|
|
|
Aliases: []string{"rm"},
|
2020-10-07 04:11:36 +00:00
|
|
|
Usage: "Remove package(s)",
|
2021-02-28 05:04:05 +00:00
|
|
|
Before: localOrToken,
|
2020-02-19 16:48:55 +00:00
|
|
|
Action: doRemove,
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 04:11:36 +00:00
|
|
|
func doRemove(_ *cli.Context) error {
|
2021-02-28 05:04:05 +00:00
|
|
|
pkgs, err := listPackages()
|
2020-10-07 04:11:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
pkgSlice := make([]string, len(pkgs))
|
|
|
|
pkgMap := make(map[string]gpm.Package)
|
|
|
|
for idx, pkg := range pkgs {
|
|
|
|
pkgSlice[idx] = pkg.Name
|
|
|
|
pkgMap[pkg.Name] = pkg
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
pkgQuestion := &survey.Select{
|
|
|
|
Message: "Select package to remove",
|
|
|
|
Options: pkgSlice,
|
|
|
|
}
|
|
|
|
|
|
|
|
var pkgName string
|
|
|
|
if err := survey.AskOne(pkgQuestion, &pkgName); err != nil {
|
2020-02-19 05:18:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
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
|
2020-02-19 05:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:04:05 +00:00
|
|
|
beaver.Infof("Removed %s", yellow.Format(pkgName))
|
2020-02-19 05:18:36 +00:00
|
|
|
return nil
|
|
|
|
}
|