git-project-manager/cmd/alias_delete.go

87 lines
2.0 KiB
Go

package cmd
import (
"fmt"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
)
var aliasDeleteCmd = &cobra.Command{
Use: "delete [fuzzy project or alias]",
Aliases: []string{"rm", "del", "d"},
Short: "Delete a project alias",
Long: aliasDeleteCmdLong,
PreRun: mustHaveAliases,
Run: runDeleteAliasCmd,
}
func runDeleteAliasCmd(cmd *cobra.Command, args []string) {
var project *projects.Project
var err error
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
MustHaveAlias: true,
}
if len(args) > 0 {
fzfOpts.Search = searchStringFromArgs(args)
project = fzfFindProject(fzfOpts)
} else {
project, err = fzfProject(fzfOpts)
}
if project == nil || err != nil {
plog.Fatal("Failed to find project to delete aliases from", plog.Args(
"error", err,
))
}
aliasStrings := cache.GetProjectAliasStrings(project)
deletionCandidates, err := pterm.DefaultInteractiveMultiselect.
WithOptions(aliasStrings).
Show()
if err != nil || len(deletionCandidates) < 1 {
plog.Fatal("Failed to find project to delete aliases from", plog.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 {
plog.Warn("Alias deletion cancelled")
continue
}
plog.Info("Deleting alias", plog.Args(
"project", project.String(),
"alias", a,
))
cache.DeleteAlias(cache.GetAliasByName(a))
}
fmt.Println(cache.ProjectString(project))
}
func init() {
aliasCmd.AddCommand(aliasDeleteCmd)
aliasDeleteCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
aliasDeleteCmd.RegisterFlagCompletionFunc("projectID", validProjectIdFunc)
viper.BindPFlag("alias.delete.projectID", aliasDeleteCmd.Flag("projectID"))
}