broken wip

This commit is contained in:
Ryan McGuire 2023-12-08 17:06:09 -05:00
parent f17ce69ef8
commit f40079cb7f
3 changed files with 22 additions and 74 deletions

View File

@ -14,6 +14,7 @@ import (
var aliasAddCmd = &cobra.Command{ var aliasAddCmd = &cobra.Command{
Use: "add", Use: "add",
Short: "Add a project alias", Short: "Add a project alias",
Args: cobra.ArbitraryArgs,
Long: aliasAddCmdLong, Long: aliasAddCmdLong,
Run: runAddAliasCmd, Run: runAddAliasCmd,
} }
@ -27,6 +28,11 @@ func runAddAliasCmd(cmd *cobra.Command, args []string) {
project = cache.GetProjectByID(projectID) project = cache.GetProjectByID(projectID)
} }
// Check by arg
if len(args) > 0 {
project = fzfFindProject(args[0])
}
// Collect by fzf // Collect by fzf
if project == nil { if project == nil {
var err error var err error

View File

@ -2,94 +2,33 @@ package cmd
import ( import (
"fmt" "fmt"
"strings"
"github.com/pterm/pterm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
) )
var aliasAddCmd = &cobra.Command{ var aliasDeleteCmd = &cobra.Command{
Use: "add", Use: "delete [fuzzy project or alias]",
Short: "Add a project alias", Short: "Delete a project alias",
Long: aliasAddCmdLong, Long: aliasDeleteCmdLong,
Run: runAddAliasCmd, Run: runDeleteAliasCmd,
} }
func runAddAliasCmd(cmd *cobra.Command, args []string) { func runDeleteAliasCmd(cmd *cobra.Command, args []string) {
var project *gitlab.Project var project *gitlab.Project
// Check by flag if len(args) > 0 {
if projectID := viper.GetInt("alias.add.projectid"); projectID > 0 { project = fzfFindProject(args[0])
plog.Debug(fmt.Sprintf("Adding for inbound project ID %d", projectID))
project = cache.GetProjectByID(projectID)
}
// Collect by fzf
if project == nil {
var err error
project, err = fzfProject(cmd.Context())
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 {
a = strings.Trim(a, " '\"%")
if a == "" {
continue
}
if err := cache.AddAlias(a, project.ID); err != nil {
plog.Debug("Skipping alias add", plog.Args(
"error", err,
"alias", a,
))
} else { } else {
plog.Info("Successfully added alias to project", plog.Args( project, _ = fzfProject(cmd.Context())
"project", project.String(),
"alias", a,
))
}
}
}
func promptAliasesForProject(p *gitlab.Project) []string {
aliases := cache.GetProjectAliases(p)
if len(aliases) > 0 {
plog.Info("Adding aliases to project", plog.Args(
"project", p.String(),
"existingAliases", projects.ProjectAliasesString(aliases),
))
} else {
pterm.Info.Printfln("Adding aliases to %s", p.Name)
} }
response, _ := pterm.DefaultInteractiveTextInput. fmt.Println(project.String())
WithMultiLine(false).
WithDefaultValue(p.Path + " ").
Show("Enter aliases separated by space")
return strings.Split(response, " ")
} }
func init() { func init() {
aliasCmd.AddCommand(aliasAddCmd) aliasCmd.AddCommand(aliasDeleteCmd)
aliasAddCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID") aliasDeleteCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
viper.BindPFlag("alias.add.projectID", aliasAddCmd.Flag("projectID")) viper.BindPFlag("alias.delete.projectID", aliasDeleteCmd.Flag("projectID"))
} }

View File

@ -12,6 +12,9 @@ listing, adding, and deleting.`
const aliasAddCmdLong = `Adds a project alias to a project const aliasAddCmdLong = `Adds a project alias to a project
project ID can be provided, or will otherwise use fuzzy find` project ID can be provided, or will otherwise use fuzzy find`
const aliasDeleteCmdLong = `Deletes aliases from projects
project ID can be provided, or will otherwise use fuzzy find`
const cacheCmdLong = `Contains sub-commands for managing project cache. const cacheCmdLong = `Contains sub-commands for managing project cache.
The project cache keeps this speedy, without smashing against the GitLab The project cache keeps this speedy, without smashing against the GitLab
API every time a new project is added / searched for` API every time a new project is added / searched for`