git-project-manager/cmd/project.go

63 lines
1.5 KiB
Go
Raw Normal View History

2023-12-08 21:52:26 +00:00
package cmd
import (
"github.com/spf13/cobra"
2023-12-09 04:13:17 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
2023-12-08 21:52:26 +00:00
"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",
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"},
ValidArgsFunction: validAliasesFunc,
Long: projCmdLong,
PersistentPreRun: initProjectCmd,
PersistentPostRun: postProjectCmd,
Run: projectCmdRun,
}
func projectCmdRun(cmd *cobra.Command, args []string) {
2023-12-09 04:13:17 +00:00
goToProject(getProject(args))
}
func getProject(args []string) *gitlab.Project {
2023-12-08 21:52:26 +00:00
var searchString string
if len(args) > 0 {
searchString = args[0]
}
2023-12-09 04:13:17 +00:00
2023-12-08 21:52:26 +00:00
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)),
))
}
2023-12-09 04:13:17 +00:00
if len(cache.GetProjectAliases(project)) == 0 {
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)
}
func postProjectCmd(cmd *cobra.Command, args []string) {
postProjectCache(cmd, args)
}
func init() {
rootCmd.AddCommand(projectCmd)
}