2020-02-19 05:18:36 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-07 04:11:36 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.jolheiser.com/gpm/config"
|
|
|
|
|
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)",
|
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 {
|
|
|
|
cfg, err := config.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-19 05:18:36 +00:00
|
|
|
pkgQuestion := &survey.Input{
|
|
|
|
Message: "Package name",
|
|
|
|
}
|
|
|
|
var pkgAnswer string
|
|
|
|
|
|
|
|
if err := survey.AskOne(pkgQuestion, &pkgAnswer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-07 04:11:36 +00:00
|
|
|
for idx, p := range cfg.Packages {
|
2020-02-19 05:18:36 +00:00
|
|
|
if strings.EqualFold(p.Name, pkgAnswer) {
|
|
|
|
confirm := &survey.Confirm{
|
2020-02-19 05:39:54 +00:00
|
|
|
Message: fmt.Sprintf("Are you sure you want to remove %s (%s) ?", p.Name, p.Import),
|
2020-02-19 05:18:36 +00:00
|
|
|
Default: false,
|
|
|
|
}
|
|
|
|
var answer bool
|
|
|
|
|
|
|
|
if err := survey.AskOne(confirm, &answer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if answer {
|
2020-10-07 04:11:36 +00:00
|
|
|
cfg.Packages = append(cfg.Packages[:idx], cfg.Packages[idx+1:]...)
|
|
|
|
if err := cfg.Save(); err != nil {
|
2020-02-19 05:18:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
beaver.Infof("Removed `%s` from local gpm.", p.Name)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
beaver.Infof("Did not remove `%s` from local gpm.", p.Name)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|