package cmd import ( "github.com/spf13/cobra" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects" ) var projectCmd = &cobra.Command{ Use: "project [fuzzy alias search]", Short: "Use a GitLab project", Aliases: []string{"proj", "projects", "p"}, Args: cobra.MaximumNArgs(1), ArgAliases: []string{"alias"}, ValidArgsFunction: validAliasesFunc, Long: projCmdLong, PersistentPreRun: initProjectCmd, PersistentPostRun: postProjectCmd, Run: projectCmdRun, } func projectCmdRun(cmd *cobra.Command, args []string) { goToProject(getProject(args)) } func getProject(args []string) *gitlab.Project { var searchString string if len(args) > 0 { searchString = args[0] } project := fzfFindProject(searchString) if project == nil { plog.Fatal("Failed to find a project, nothing to do") } else { plog.Info("Houston, we have a project", plog.Args( "project", project.String(), "aliases", projects.ProjectAliasesString( cache.GetProjectAliases(project)), )) } if len(cache.GetProjectAliases(project)) == 0 { plog.Info("New project, set aliases or press enter for default") addNewAliases(project.ID) } return project } func initProjectCmd(cmd *cobra.Command, args []string) { initProjectCache(cmd, args) } func postProjectCmd(cmd *cobra.Command, args []string) { postProjectCache(cmd, args) } func init() { rootCmd.AddCommand(projectCmd) }