70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
|
|
)
|
|
|
|
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"},
|
|
Long: projCmdLong,
|
|
PersistentPreRun: initProjectCmd,
|
|
PersistentPostRun: postProjectCmd,
|
|
// Run: projectGoCmdRun,
|
|
}
|
|
|
|
func getProject(args []string) *remotes.Project {
|
|
gitlabs := viper.GetStringSlice("project.gitlabs")
|
|
fzfOpts := &fzfProjectOpts{
|
|
Ctx: rootCmd.Context(),
|
|
Search: searchStringFromArgs(args),
|
|
Gitlabs: gitlabs,
|
|
}
|
|
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", 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)
|
|
mustHaveProjects(cmd, args)
|
|
}
|
|
|
|
func postProjectCmd(cmd *cobra.Command, args []string) {
|
|
postProjectCache(cmd, args)
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(projectCmd)
|
|
projectCmd.PersistentFlags().StringArray("gitlab", []string{}, "Specify gitlab remote, provide flag multiple times")
|
|
projectCmd.RegisterFlagCompletionFunc("gitlab", validGitlabRemotesFunc)
|
|
viper.BindPFlag("project.gitlabs", projectCmd.Flag("gitlab"))
|
|
}
|
|
|
|
func mustHaveProjects(cmd *cobra.Command, args []string) {
|
|
if len(cache.Projects) == 0 {
|
|
plog.Fatal("No projects to " + cmd.Name() + ", try running cache load")
|
|
}
|
|
}
|