git-project-manager/cmd/alias_delete.go

87 lines
2.0 KiB
Go
Raw Normal View History

2023-12-08 21:52:26 +00:00
package cmd
import (
"fmt"
2023-12-09 04:13:17 +00:00
"github.com/pterm/pterm"
2023-12-08 21:52:26 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2024-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
2023-12-08 21:52:26 +00:00
)
2023-12-08 22:06:09 +00:00
var aliasDeleteCmd = &cobra.Command{
2023-12-09 04:13:17 +00:00
Use: "delete [fuzzy project or alias]",
Aliases: []string{"rm", "del", "d"},
Short: "Delete a project alias",
Long: aliasDeleteCmdLong,
2023-12-10 15:10:46 +00:00
PreRun: mustHaveAliases,
2023-12-09 04:13:17 +00:00
Run: runDeleteAliasCmd,
2023-12-08 21:52:26 +00:00
}
2023-12-08 22:06:09 +00:00
func runDeleteAliasCmd(cmd *cobra.Command, args []string) {
2024-01-15 20:39:35 +00:00
var project *projects.Project
2023-12-09 04:13:17 +00:00
var err error
2023-12-08 21:52:26 +00:00
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
MustHaveAlias: true,
}
2023-12-08 22:06:09 +00:00
if len(args) > 0 {
fzfOpts.Search = searchStringFromArgs(args)
project = fzfFindProject(fzfOpts)
2023-12-08 21:52:26 +00:00
} else {
project, err = fzfProject(fzfOpts)
2023-12-08 21:52:26 +00:00
}
2023-12-09 04:13:17 +00:00
if project == nil || err != nil {
plog.Fatal("Failed to find project to delete aliases from", plog.Args(
"error", err,
))
}
2024-01-15 21:02:15 +00:00
aliasStrings := projectCache.GetProjectAliasStrings(project)
2023-12-09 04:13:17 +00:00
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,
))
2024-01-15 21:02:15 +00:00
projectCache.DeleteAlias(projectCache.GetAliasByName(a))
2023-12-09 04:13:17 +00:00
}
2024-01-15 21:02:15 +00:00
fmt.Println(projectCache.ProjectString(project))
2023-12-08 21:52:26 +00:00
}
func init() {
2023-12-08 22:06:09 +00:00
aliasCmd.AddCommand(aliasDeleteCmd)
aliasDeleteCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
aliasDeleteCmd.RegisterFlagCompletionFunc("projectID", validProjectIdFunc)
2023-12-08 22:06:09 +00:00
viper.BindPFlag("alias.delete.projectID", aliasDeleteCmd.Flag("projectID"))
2023-12-08 21:52:26 +00:00
}