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

87 lines
2.1 KiB
Go
Raw Normal View History

2024-12-30 19:54:32 +00:00
package alias
2023-12-08 21:52:26 +00:00
import (
"fmt"
2024-12-30 19:54:32 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
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"
)
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",
2024-12-30 19:54:32 +00:00
Long: util.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
2024-12-30 19:54:32 +00:00
fzfOpts := &util.FzfProjectOpts{
Ctx: cmd.Context(),
MustHaveAlias: true,
}
2023-12-08 22:06:09 +00:00
if len(args) > 0 {
2024-12-30 19:54:32 +00:00
fzfOpts.Search = utils.SearchStringFromArgs(args)
project = utils.FzfFindProject(fzfOpts)
2023-12-08 21:52:26 +00:00
} else {
2024-12-30 19:54:32 +00:00
project, err = utils.FzfProject(fzfOpts)
2023-12-08 21:52:26 +00:00
}
2023-12-09 04:13:17 +00:00
if project == nil || err != nil {
2024-12-30 19:54:32 +00:00
utils.Logger().Fatal("Failed to find project to delete aliases from", utils.Logger().Args(
2023-12-09 04:13:17 +00:00
"error", err,
))
}
2024-12-30 19:54:32 +00:00
aliasStrings := utils.Cache().GetProjectAliasStrings(project)
2023-12-09 04:13:17 +00:00
deletionCandidates, err := pterm.DefaultInteractiveMultiselect.
WithOptions(aliasStrings).
Show()
if err != nil || len(deletionCandidates) < 1 {
2024-12-30 19:54:32 +00:00
utils.Logger().Fatal("Failed to find project to delete aliases from", utils.Logger().Args(
2023-12-09 04:13:17 +00:00
"error", err,
))
}
for _, a := range deletionCandidates {
confirm, _ := pterm.DefaultInteractiveConfirm.
WithDefaultText(fmt.Sprintf("Really delete %s -> %s?",
a, project.String())).
WithConfirmText("y").
Show()
if !confirm {
2024-12-30 19:54:32 +00:00
utils.Logger().Warn("Alias deletion cancelled")
2023-12-09 04:13:17 +00:00
continue
}
2024-12-30 19:54:32 +00:00
utils.Logger().Info("Deleting alias", utils.Logger().Args(
2023-12-09 04:13:17 +00:00
"project", project.String(),
"alias", a,
))
2024-12-30 19:54:32 +00:00
utils.Cache().DeleteAlias(utils.Cache().GetAliasByName(a))
2023-12-09 04:13:17 +00:00
}
2024-12-30 19:54:32 +00:00
fmt.Println(utils.Cache().ProjectString(project))
2023-12-08 21:52:26 +00:00
}
func init() {
2023-12-08 22:06:09 +00:00
aliasDeleteCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
2024-12-30 19:54:32 +00:00
aliasDeleteCmd.RegisterFlagCompletionFunc("projectID", util.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
}