2023-12-08 21:52:26 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pterm/pterm"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2024-01-15 20:39:35 +00:00
|
|
|
cacheProjects "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
|
|
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-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,
|
|
|
|
Long: aliasAddCmdLong,
|
|
|
|
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
|
|
|
|
if projectID := viper.GetInt("alias.add.projectid"); projectID > 0 {
|
|
|
|
plog.Debug(fmt.Sprintf("Adding for inbound project ID %d", projectID))
|
|
|
|
project = cache.GetProjectByID(projectID)
|
|
|
|
}
|
|
|
|
|
2023-12-08 22:06:09 +00:00
|
|
|
// Check by arg
|
|
|
|
if len(args) > 0 {
|
2024-01-14 15:33:15 +00:00
|
|
|
project = fzfFindProject(&fzfProjectOpts{Ctx: cmd.Context(), Search: 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-01-14 15:33:15 +00:00
|
|
|
project, err = fzfProject(&fzfProjectOpts{Ctx: cmd.Context()})
|
2023-12-08 21:52:26 +00:00
|
|
|
if err != nil || project == nil {
|
|
|
|
plog.Fatal("No project to alias, nothing to do", plog.Args("error", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addNewAliases(project.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addNewAliases(projectID int) {
|
|
|
|
project := cache.GetProjectByID(projectID)
|
|
|
|
if project == nil {
|
|
|
|
plog.Error("Failed to find project to alias", plog.Args("projectID", projectID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the aliases
|
|
|
|
aliases := promptAliasesForProject(project)
|
|
|
|
|
|
|
|
// 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-01-14 15:33:15 +00:00
|
|
|
if err := cache.AddAlias(a, project.ID, project.Remote); err != nil {
|
2023-12-08 21:52:26 +00:00
|
|
|
plog.Debug("Skipping alias add", plog.Args(
|
|
|
|
"error", err,
|
|
|
|
"alias", a,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
plog.Info("Successfully added alias to project", plog.Args(
|
|
|
|
"project", project.String(),
|
|
|
|
"alias", a,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 20:39:35 +00:00
|
|
|
func promptAliasesForProject(p *projects.Project) []string {
|
2023-12-08 21:52:26 +00:00
|
|
|
aliases := cache.GetProjectAliases(p)
|
|
|
|
if len(aliases) > 0 {
|
|
|
|
plog.Info("Adding aliases to project", plog.Args(
|
|
|
|
"project", p.String(),
|
2024-01-15 20:39:35 +00:00
|
|
|
"existingAliases", cacheProjects.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() {
|
|
|
|
aliasCmd.AddCommand(aliasAddCmd)
|
|
|
|
aliasAddCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
|
2024-01-14 15:33:15 +00:00
|
|
|
|
|
|
|
aliasAddCmd.RegisterFlagCompletionFunc("projectID", validProjectIdFunc)
|
|
|
|
|
2023-12-08 21:52:26 +00:00
|
|
|
viper.BindPFlag("alias.add.projectID", aliasAddCmd.Flag("projectID"))
|
|
|
|
}
|