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

View File

@ -2,94 +2,33 @@ package cmd
import (
"fmt"
"strings"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"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/projects"
)
var aliasAddCmd = &cobra.Command{
Use: "add",
Short: "Add a project alias",
Long: aliasAddCmdLong,
Run: runAddAliasCmd,
var aliasDeleteCmd = &cobra.Command{
Use: "delete [fuzzy project or alias]",
Short: "Delete a project alias",
Long: aliasDeleteCmdLong,
Run: runDeleteAliasCmd,
}
func runAddAliasCmd(cmd *cobra.Command, args []string) {
func runDeleteAliasCmd(cmd *cobra.Command, args []string) {
var project *gitlab.Project
// 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)
}
// 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 {
plog.Info("Successfully added alias to project", plog.Args(
"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),
))
if len(args) > 0 {
project = fzfFindProject(args[0])
} else {
pterm.Info.Printfln("Adding aliases to %s", p.Name)
project, _ = fzfProject(cmd.Context())
}
response, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(false).
WithDefaultValue(p.Path + " ").
Show("Enter aliases separated by space")
return strings.Split(response, " ")
fmt.Println(project.String())
}
func init() {
aliasCmd.AddCommand(aliasAddCmd)
aliasAddCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
viper.BindPFlag("alias.add.projectID", aliasAddCmd.Flag("projectID"))
aliasCmd.AddCommand(aliasDeleteCmd)
aliasDeleteCmd.PersistentFlags().Int("projectID", 0, "Specify a project by ID")
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
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.
The project cache keeps this speedy, without smashing against the GitLab
API every time a new project is added / searched for`