gpm/cmd/remove.go

57 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"gitea.com/gpm/gpm/modules/config"
"gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"strings"
)
var Remove = cli.Command{
Name: "remove",
Aliases: []string{"rm"},
Usage: "Remove a package",
Action: doRemove,
}
func doRemove(ctx *cli.Context) error {
pkgQuestion := &survey.Input{
Message: "Package name",
}
var pkgAnswer string
if err := survey.AskOne(pkgQuestion, &pkgAnswer); err != nil {
return err
}
for idx, p := range config.Packages {
if strings.EqualFold(p.Name, pkgAnswer) {
confirm := &survey.Confirm{
Message: fmt.Sprintf("Are you sure you want to remove %s (%s) ?", p.Name, p.Import),
Default: false,
}
var answer bool
if err := survey.AskOne(confirm, &answer); err != nil {
return err
}
if answer {
config.Packages = append(config.Packages[:idx], config.Packages[idx+1:]...)
if err := config.Save(); err != nil {
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
}