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

109 lines
2.9 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"
"strings"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2024-10-01 18:29:14 +00:00
2024-12-30 19:54:32 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
2024-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/cache"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
2023-12-08 21:52:26 +00:00
)
var aliasAddCmd = &cobra.Command{
2023-12-09 04:13:17 +00:00
Use: "add",
Aliases: []string{"set", "a", "s"},
Short: "Add a project alias",
Args: cobra.ArbitraryArgs,
2024-12-30 19:54:32 +00:00
Long: util.AliasAddCmdLong,
2023-12-09 04:13:17 +00:00
Run: runAddAliasCmd,
2023-12-08 21:52:26 +00:00
}
func runAddAliasCmd(cmd *cobra.Command, args []string) {
2024-01-15 20:39:35 +00:00
var project *projects.Project
2023-12-08 21:52:26 +00:00
// Check by flag
2024-12-30 19:54:32 +00:00
if projectID := viper.GetInt(util.ViperAliasAddPID); projectID > 0 {
utils.Logger().Debug(fmt.Sprintf("Adding for inbound project ID %d", projectID))
project = utils.Cache().GetProjectByID(projectID)
2023-12-08 21:52:26 +00:00
}
2023-12-08 22:06:09 +00:00
// Check by arg
if len(args) > 0 {
2024-12-30 19:54:32 +00:00
project = utils.FzfFindProject(&util.FzfProjectOpts{Ctx: cmd.Context(), Search: utils.SearchStringFromArgs(args)})
2023-12-08 22:06:09 +00:00
}
2023-12-08 21:52:26 +00:00
// Collect by fzf
if project == nil {
var err error
2024-12-30 19:54:32 +00:00
project, err = utils.FzfProject(&util.FzfProjectOpts{Ctx: cmd.Context()})
2023-12-08 21:52:26 +00:00
if err != nil || project == nil {
2024-12-30 19:54:32 +00:00
utils.Logger().Fatal("No project to alias, nothing to do", utils.Logger().Args("error", err))
2023-12-08 21:52:26 +00:00
}
}
2024-12-30 20:50:31 +00:00
AddNewAliases(cmd, project.ID)
2023-12-08 21:52:26 +00:00
}
2024-12-30 20:50:31 +00:00
func AddNewAliases(cmd *cobra.Command, projectID int) {
u := util.MustFromCtx(cmd.Context())
project := u.Cache().GetProjectByID(projectID)
2023-12-08 21:52:26 +00:00
if project == nil {
2024-12-30 20:50:31 +00:00
u.Logger().Error("Failed to find project to alias", u.Logger().Args("projectID", projectID))
2023-12-08 21:52:26 +00:00
return
}
// Collect the aliases
2024-12-30 20:50:31 +00:00
aliases := PromptAliasesForProject(cmd, project)
2023-12-08 21:52:26 +00:00
// Add aliases
for _, a := range aliases {
2023-12-09 04:13:17 +00:00
a = strings.Trim(a, " '\"%<>|`")
2023-12-08 21:52:26 +00:00
if a == "" {
continue
}
2024-12-30 20:50:31 +00:00
if err := u.Cache().AddAlias(a, project.ID, project.Remote); err != nil {
u.Logger().Debug("Skipping alias add", u.Logger().Args(
2023-12-08 21:52:26 +00:00
"error", err,
"alias", a,
))
} else {
2024-12-30 20:50:31 +00:00
u.Logger().Info("Successfully added alias to project", u.Logger().Args(
2023-12-08 21:52:26 +00:00
"project", project.String(),
"alias", a,
))
}
}
}
2024-12-30 20:50:31 +00:00
func PromptAliasesForProject(cmd *cobra.Command, p *projects.Project) []string {
u := util.MustFromCtx(cmd.Context())
aliases := u.Cache().GetProjectAliases(p)
2023-12-08 21:52:26 +00:00
if len(aliases) > 0 {
2024-12-30 20:50:31 +00:00
u.Logger().Info("Adding aliases to project", u.Logger().Args(
2023-12-08 21:52:26 +00:00
"project", p.String(),
2024-01-15 21:02:15 +00:00
"existingAliases", cache.ProjectAliasesString(aliases),
2023-12-08 21:52:26 +00:00
))
} else {
pterm.Info.Printfln("Adding aliases to %s", p.Name)
}
response, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(false).
WithDefaultValue(p.Path + " ").
Show("Enter aliases separated by space")
return strings.Split(response, " ")
}
func init() {
2024-12-30 19:54:32 +00:00
aliasAddCmd.PersistentFlags().Int(util.FlagProjectID, 0, "Specify a project by ID")
2024-12-30 19:54:32 +00:00
aliasAddCmd.RegisterFlagCompletionFunc(util.FlagProjectID, util.ValidProjectIdFunc)
2024-12-30 19:54:32 +00:00
viper.BindPFlag(util.ViperAliasAddPID, aliasAddCmd.Flag(util.FlagProjectID))
2023-12-08 21:52:26 +00:00
}