git-project-manager/cmd/project.go

68 lines
1.7 KiB
Go
Raw Normal View History

2023-12-08 21:52:26 +00:00
package cmd
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
2024-10-01 18:29:14 +00:00
2024-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/cache"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
2023-12-08 21:52:26 +00:00
)
var projectCmd = &cobra.Command{
Use: "project [fuzzy alias search]",
Short: "Use a GitLab project",
2023-12-09 04:13:17 +00:00
Aliases: []string{"proj", "projects", "p"},
2023-12-08 21:52:26 +00:00
Args: cobra.MaximumNArgs(1),
ArgAliases: []string{"alias"},
Long: projCmdLong,
PersistentPreRun: initProjectCmd,
PersistentPostRun: postProjectCmd,
2023-12-10 04:19:19 +00:00
// Run: projectGoCmdRun,
2023-12-09 04:13:17 +00:00
}
2024-01-15 20:39:35 +00:00
func getProject(args []string) *projects.Project {
2024-10-01 18:29:14 +00:00
remotes := viper.GetStringSlice(FlagRemote)
fzfOpts := &fzfProjectOpts{
Ctx: rootCmd.Context(),
Search: searchStringFromArgs(args),
Remotes: remotes,
}
project := fzfFindProject(fzfOpts)
2023-12-08 21:52:26 +00:00
if project == nil {
plog.Fatal("Failed to find a project, nothing to do")
} else {
2023-12-10 04:19:19 +00:00
plog.Debug("Houston, we have a project", plog.Args(
2023-12-08 21:52:26 +00:00
"project", project.String(),
2024-01-15 21:02:15 +00:00
"aliases", cache.ProjectAliasesString(
projectCache.GetProjectAliases(project)),
2023-12-08 21:52:26 +00:00
))
}
2023-12-09 04:13:17 +00:00
2024-01-15 21:02:15 +00:00
if len(projectCache.GetProjectAliases(project)) == 0 {
2023-12-09 04:13:17 +00:00
plog.Info("New project, set aliases or press enter for default")
addNewAliases(project.ID)
}
return project
2023-12-08 21:52:26 +00:00
}
func initProjectCmd(cmd *cobra.Command, args []string) {
initProjectCache(cmd, args)
2023-12-10 15:10:46 +00:00
mustHaveProjects(cmd, args)
2023-12-08 21:52:26 +00:00
}
func postProjectCmd(cmd *cobra.Command, args []string) {
postProjectCache(cmd, args)
}
func init() {
rootCmd.AddCommand(projectCmd)
}
2023-12-10 15:10:46 +00:00
func mustHaveProjects(cmd *cobra.Command, args []string) {
2024-01-15 21:02:15 +00:00
if len(projectCache.Projects) == 0 {
2023-12-10 15:10:46 +00:00
plog.Fatal("No projects to " + cmd.Name() + ", try running cache load")
}
}