git-project-manager/cmd/alias/alias_delete.go

87 lines
2.1 KiB
Go

package alias
import (
"fmt"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var aliasDeleteCmd = &cobra.Command{
Use: "delete [fuzzy project or alias]",
Aliases: []string{"rm", "del", "d"},
Short: "Delete a project alias",
Long: util.AliasDeleteCmdLong,
PreRun: mustHaveAliases,
Run: runDeleteAliasCmd,
}
func runDeleteAliasCmd(cmd *cobra.Command, args []string) {
var project *projects.Project
var err error
fzfOpts := &util.FzfProjectOpts{
Ctx: cmd.Context(),
MustHaveAlias: true,
}
if len(args) > 0 {
fzfOpts.Search = utils.SearchStringFromArgs(args)
project = utils.FzfFindProject(fzfOpts)
} else {
project, err = utils.FzfProject(fzfOpts)
}
if project == nil || err != nil {
utils.Logger().Fatal("Failed to find project to delete aliases from", utils.Logger().Args(
"error", err,
))
}
aliasStrings := utils.Cache().GetProjectAliasStrings(project)
deletionCandidates, err := pterm.DefaultInteractiveMultiselect.
WithOptions(aliasStrings).
Show()
if err != nil || len(deletionCandidates) < 1 {
utils.Logger().Fatal("Failed to find project to delete aliases from", utils.Logger().Args(
"error", err,
))
}
for _, a := range deletionCandidates {
confirm, _ := pterm.DefaultInteractiveConfirm.
WithDefaultText(fmt.Sprintf("Really delete %s -> %s?",
a, project.String())).
WithConfirmText("y").
Show()
if !confirm {
utils.Logger().Warn("Alias deletion cancelled")
continue
}
utils.Logger().Info("Deleting alias", utils.Logger().Args(
"project", project.String(),
"alias", a,
))
utils.Cache().DeleteAlias(utils.Cache().GetAliasByName(a))
}
fmt.Println(utils.Cache().ProjectString(project))
}
func init() {
aliasDeleteCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
aliasDeleteCmd.RegisterFlagCompletionFunc("projectID", util.ValidProjectIdFunc)
viper.BindPFlag("alias.delete.projectID", aliasDeleteCmd.Flag("projectID"))
}