package cmd import ( "github.com/spf13/cobra" "github.com/spf13/viper" "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/cache" "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects" ) var projectCmd = &cobra.Command{ Use: "project [fuzzy alias search]", Short: "Use a Git project", Aliases: []string{"proj", "projects", "p"}, Args: cobra.MaximumNArgs(1), ArgAliases: []string{"alias"}, Long: projCmdLong, PersistentPreRun: initProjectCmd, PersistentPostRun: postProjectCmd, // Run: projectGoCmdRun, } func getProject(args []string) *projects.Project { remotes := viper.GetStringSlice(FlagRemote) fzfOpts := &fzfProjectOpts{ Ctx: rootCmd.Context(), Search: searchStringFromArgs(args), Remotes: remotes, } project := fzfFindProject(fzfOpts) if project == nil { plog.Fatal("Failed to find a project, nothing to do") } else { plog.Debug("Houston, we have a project", plog.Args( "project", project.String(), "aliases", cache.ProjectAliasesString( projectCache.GetProjectAliases(project)), )) } if len(projectCache.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) mustHaveProjects(cmd, args) } func postProjectCmd(cmd *cobra.Command, args []string) { postProjectCache(cmd, args) } func init() { rootCmd.AddCommand(projectCmd) } func mustHaveProjects(cmd *cobra.Command, _ []string) { if len(projectCache.Projects) == 0 { plog.Fatal("No projects to " + cmd.Name() + ", try running cache load") } }