package alias import ( "fmt" "strings" "github.com/pterm/pterm" "github.com/spf13/cobra" "github.com/spf13/viper" "gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util" "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/cache" "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects" ) var aliasAddCmd = &cobra.Command{ Use: "add", Aliases: []string{"set", "a", "s"}, Short: "Add a project alias", Args: cobra.ArbitraryArgs, Long: util.AliasAddCmdLong, Run: runAddAliasCmd, } func runAddAliasCmd(cmd *cobra.Command, args []string) { var project *projects.Project // Check by flag if projectID := viper.GetInt(util.ViperAliasAddPID); projectID > 0 { utils.Logger().Debug(fmt.Sprintf("Adding for inbound project ID %d", projectID)) projects := utils.Cache().GetProjectsByID(projectID) if len(projects) > 0 { project = projects[0] } if len(projects) > 1 { utils.Logger(). Warn(fmt.Sprintf("found %d remotes with same ID, using first remote", len(projects))) } } // Check by arg if len(args) > 0 { project = utils.FzfFindProject(&util.FzfProjectOpts{Ctx: cmd.Context(), Search: utils.SearchStringFromArgs(args)}) } // Collect by fzf if project == nil { var err error project, err = utils.FzfProject(&util.FzfProjectOpts{Ctx: cmd.Context()}) if err != nil || project == nil { utils.Logger().Fatal("No project to alias, nothing to do", utils.Logger().Args("error", err)) } } AddNewAliases(cmd, project.GetID()) } func AddNewAliases(cmd *cobra.Command, projectID string) { u := util.MustFromCtx(cmd.Context()) project := u.Cache().GetProjectByID(projectID) if project == nil { u.Logger().Error("Failed to find project to alias", u.Logger().Args("projectID", projectID)) return } // Collect the aliases aliases := PromptAliasesForProject(cmd, project) // Add aliases for _, a := range aliases { a = strings.Trim(a, " '\"%<>|`") if a == "" { continue } if err := u.Cache().AddAlias(a, project); err != nil { u.Logger().Debug("Skipping alias add", u.Logger().Args( "error", err, "alias", a, )) } else { u.Logger().Info("Successfully added alias to project", u.Logger().Args( "project", project.String(), "alias", a, )) } } } func PromptAliasesForProject(cmd *cobra.Command, p *projects.Project) []string { u := util.MustFromCtx(cmd.Context()) aliases := u.Cache().GetProjectAliases(p) if len(aliases) > 0 { u.Logger().Info("Adding aliases to project", u.Logger().Args( "project", p.String(), "existingAliases", cache.ProjectAliasesString(aliases), )) } 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() { aliasAddCmd.PersistentFlags().Int(util.FlagProjectID, 0, "Specify a project by ID") aliasAddCmd.RegisterFlagCompletionFunc(util.FlagProjectID, util.ValidProjectIdFunc) viper.BindPFlag(util.ViperAliasAddPID, aliasAddCmd.Flag(util.FlagProjectID)) }