Move from --gitlab flags to global --remote flags
This commit is contained in:
@ -13,13 +13,16 @@ var dumpCmd = &cobra.Command{
|
||||
Long: `Dumps cache to display`,
|
||||
PreRun: runCacheCmd,
|
||||
PostRun: postCacheCmd,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if conf.Dump.Full {
|
||||
fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args)))
|
||||
} else {
|
||||
plog.Info(projectCache.String())
|
||||
}
|
||||
},
|
||||
Run: runCacheDunpCmd,
|
||||
}
|
||||
|
||||
func runCacheDunpCmd(cmd *cobra.Command, args []string) {
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
if conf.Dump.Full {
|
||||
fmt.Println(projectCache.DumpString(true, searchStringFromArgs(args), remotes...))
|
||||
} else {
|
||||
plog.Info(projectCache.String())
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -17,7 +17,8 @@ wants to find a new project.`,
|
||||
}
|
||||
|
||||
func loadCache(cmd *cobra.Command, args []string) {
|
||||
projectCache.Refresh()
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
projectCache.Refresh(remotes...)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -20,11 +20,11 @@ var projectCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func getProject(args []string) *projects.Project {
|
||||
gitlabs := viper.GetStringSlice("project.gitlabs")
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
fzfOpts := &fzfProjectOpts{
|
||||
Ctx: rootCmd.Context(),
|
||||
Search: searchStringFromArgs(args),
|
||||
Gitlabs: gitlabs,
|
||||
Remotes: remotes,
|
||||
}
|
||||
project := fzfFindProject(fzfOpts)
|
||||
|
||||
@ -57,9 +57,6 @@ func postProjectCmd(cmd *cobra.Command, args []string) {
|
||||
|
||||
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) {
|
||||
|
@ -21,12 +21,12 @@ var projectGoCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func projectGoCmdRun(cmd *cobra.Command, args []string) {
|
||||
gitlabs := viper.GetStringSlice("project.gitlabs")
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
fzfOpts := &fzfProjectOpts{
|
||||
Ctx: cmd.Context(),
|
||||
Search: searchStringFromArgs(args),
|
||||
MustHaveAlias: true,
|
||||
Gitlabs: gitlabs,
|
||||
Remotes: remotes,
|
||||
}
|
||||
project := fzfSearchProjectAliases(fzfOpts)
|
||||
|
||||
|
@ -16,8 +16,8 @@ var projectListCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func projectListCmdRun(cmd *cobra.Command, args []string) {
|
||||
gitlabs := viper.GetStringSlice("project.gitlabs")
|
||||
fmt.Println(projectCache.DumpString(viper.GetBool("project.list.all"), searchStringFromArgs(args), gitlabs...))
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
fmt.Println(projectCache.DumpString(viper.GetBool("project.list.all"), searchStringFromArgs(args), remotes...))
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -51,11 +51,11 @@ func projectOpenCmdRun(cmd *cobra.Command, args []string) {
|
||||
plog.Fatal("No usable editor found")
|
||||
}
|
||||
|
||||
gitlabs := viper.GetStringSlice("project.gitlabs")
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
fzfOpts := &fzfProjectOpts{
|
||||
Ctx: cmd.Context(),
|
||||
Search: searchStringFromArgs(args),
|
||||
Gitlabs: gitlabs,
|
||||
Remotes: remotes,
|
||||
}
|
||||
project := fzfCwdOrSearchProjectAliases(fzfOpts)
|
||||
if project == nil {
|
||||
|
@ -18,11 +18,11 @@ var projectRunCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func projectRunCmdRun(cmd *cobra.Command, args []string) {
|
||||
gitlabs := viper.GetStringSlice("project.gitlabs")
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
fzfOpts := &fzfProjectOpts{
|
||||
Ctx: cmd.Context(),
|
||||
Search: searchStringFromArgs(args),
|
||||
Gitlabs: gitlabs,
|
||||
Remotes: remotes,
|
||||
}
|
||||
project := fzfCwdOrSearchProjectAliases(fzfOpts)
|
||||
if project == nil {
|
||||
|
@ -23,11 +23,11 @@ func projectShowCmdRun(cmd *cobra.Command, args []string) {
|
||||
var project *projects.Project
|
||||
var inCwd bool
|
||||
|
||||
gitlabs := viper.GetStringSlice("project.gitlabs")
|
||||
remotes := viper.GetStringSlice("remote")
|
||||
fzfOpts := &fzfProjectOpts{
|
||||
Ctx: cmd.Context(),
|
||||
Search: searchStringFromArgs(args),
|
||||
Gitlabs: gitlabs,
|
||||
Remotes: remotes,
|
||||
}
|
||||
|
||||
// Try to find project from current directory
|
||||
|
17
cmd/root.go
17
cmd/root.go
@ -10,6 +10,8 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
|
||||
gitearemote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitea"
|
||||
gitlabremote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitlab"
|
||||
)
|
||||
|
||||
var conf config.Config
|
||||
@ -47,6 +49,7 @@ func init() {
|
||||
cobra.EnableTraverseRunHooks = true
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Global flags
|
||||
rootCmd.PersistentFlags().String("config", "",
|
||||
"config file (default is "+defConfigPath+")")
|
||||
rootCmd.PersistentFlags().String("projectPath", "",
|
||||
@ -56,7 +59,9 @@ func init() {
|
||||
rootCmd.PersistentFlags().StringSlice("remote", []string{},
|
||||
"Specify remotes by host for any sub-command. Provide multiple times or comma delimited.")
|
||||
|
||||
// Flag autocompletion
|
||||
rootCmd.RegisterFlagCompletionFunc("logLevel", validLogLevelsFunc)
|
||||
rootCmd.RegisterFlagCompletionFunc("remote", validRemotesFunc)
|
||||
|
||||
viper.BindPFlags(rootCmd.PersistentFlags())
|
||||
}
|
||||
@ -106,6 +111,7 @@ func initConfig() {
|
||||
}
|
||||
|
||||
checkConfigPerms(viper.ConfigFileUsed()) // Abort on world-readable config
|
||||
setConfigFields() // Final chance to update config struct
|
||||
|
||||
plog.Debug("Configuration loaded", plog.Args("conf", conf))
|
||||
}
|
||||
@ -127,6 +133,17 @@ func getPtermLogLevel(level string) pterm.LogLevel {
|
||||
return pLevel
|
||||
}
|
||||
|
||||
// Do any post-processing of configuration here
|
||||
func setConfigFields() {
|
||||
// Load Gitlabs
|
||||
glRemotes := gitlabremote.GitlabRemote{}
|
||||
conf.Remotes = append(conf.Remotes, glRemotes.GetInfos(conf)...)
|
||||
|
||||
// Load Giteas
|
||||
giteaRemotes := gitearemote.GiteaRemote{}
|
||||
conf.Remotes = append(conf.Remotes, giteaRemotes.GetInfos(conf)...)
|
||||
}
|
||||
|
||||
// Don't allow world-readable configuration
|
||||
func checkConfigPerms(file string) {
|
||||
stat, err := os.Stat(file)
|
||||
|
@ -29,22 +29,8 @@ func validProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete st
|
||||
|
||||
func validRemotesFunc(cmd *cobra.Command, args []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective) {
|
||||
var ttlRemotes int
|
||||
ttlRemotes += len(conf.Gitlabs)
|
||||
ttlRemotes += len(conf.Giteas)
|
||||
remotes := make([]string, 0, ttlRemotes)
|
||||
for _, remote := range conf.Gitlabs {
|
||||
if strings.HasPrefix(remote.Host, toComplete) {
|
||||
remotes = append(remotes, remote.Host)
|
||||
}
|
||||
}
|
||||
return remotes, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func validGitlabRemotesFunc(cmd *cobra.Command, args []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective) {
|
||||
remotes := make([]string, 0, len(conf.Gitlabs))
|
||||
for _, remote := range conf.Gitlabs {
|
||||
remotes := make([]string, 0, len(conf.Remotes))
|
||||
for _, remote := range conf.Remotes {
|
||||
if strings.HasPrefix(remote.Host, toComplete) {
|
||||
remotes = append(remotes, remote.Host)
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ type fzfProjectOpts struct {
|
||||
Ctx context.Context
|
||||
Search string
|
||||
MustHaveAlias bool
|
||||
Gitlabs []string
|
||||
Remotes []string
|
||||
}
|
||||
|
||||
// This will try to find a project by alias if a search term
|
||||
@ -52,7 +52,7 @@ func fzfCwdOrSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
|
||||
func fzfSearchProjectAliases(opts *fzfProjectOpts) *projects.Project {
|
||||
var project *projects.Project
|
||||
var alias *cache.ProjectAlias
|
||||
if alias = projectCache.GetAliasByName(opts.Search, opts.Gitlabs...); alias != nil {
|
||||
if alias = projectCache.GetAliasByName(opts.Search, opts.Remotes...); alias != nil {
|
||||
project = projectCache.GetProjectByAlias(alias)
|
||||
plog.Info("Perfect alias match... flawless")
|
||||
} else {
|
||||
@ -130,8 +130,8 @@ func fzfProject(opts *fzfProjectOpts) (*projects.Project, error) {
|
||||
} else {
|
||||
searchableProjects = projectCache.Projects
|
||||
}
|
||||
// Filter out unwanted gitlabs if provided
|
||||
searchableProjects = filterProjectsWithGitlabs(searchableProjects, opts.Gitlabs...)
|
||||
// Filter out unwanted remotes if provided
|
||||
searchableProjects = filterProjectsWithRemotes(searchableProjects, opts.Remotes...)
|
||||
return fzfProjectFromProjects(opts, searchableProjects)
|
||||
}
|
||||
|
||||
@ -162,11 +162,11 @@ func fzfPreviewWindow(i, w, h int) string {
|
||||
return projectCache.ProjectString(p)
|
||||
}
|
||||
|
||||
func filterProjectsWithGitlabs(gitProjects []*projects.Project, gitlabs ...string) []*projects.Project {
|
||||
func filterProjectsWithRemotes(gitProjects []*projects.Project, remotes ...string) []*projects.Project {
|
||||
filteredProjects := make([]*projects.Project, 0, len(gitProjects))
|
||||
if len(gitlabs) > 0 {
|
||||
if len(remotes) > 0 {
|
||||
for _, p := range gitProjects {
|
||||
if slices.Contains(gitlabs, p.Remote) {
|
||||
if slices.Contains(remotes, p.Remote) {
|
||||
filteredProjects = append(filteredProjects, p)
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,6 @@ import (
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
|
||||
gitearemote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitea"
|
||||
gitlabremote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitlab"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
@ -28,12 +27,7 @@ func initProjectCache(cmd *cobra.Command, args []string) {
|
||||
conf.Cache.File = conf.ProjectPath + "/.cache.yaml"
|
||||
|
||||
gitRemotes := remotes.NewRemotes()
|
||||
|
||||
// Load GitLabs
|
||||
gitRemotes.AddRemotes(getGitLabRemotes(cmd)...)
|
||||
|
||||
// Load Giteas
|
||||
gitRemotes.AddRemotes(getGiteaRemotes(cmd)...)
|
||||
gitRemotes.AddRemotes(getRemotes(cmd)...)
|
||||
|
||||
cacheOpts := &cache.CacheOpts{
|
||||
ProjectsPath: conf.ProjectPath,
|
||||
@ -56,68 +50,30 @@ func initProjectCache(cmd *cobra.Command, args []string) {
|
||||
plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes))
|
||||
}
|
||||
|
||||
// Loads all configured giteas
|
||||
func getGiteaRemotes(cmd *cobra.Command) []remote.Remote {
|
||||
// Generically loads remotes from info.RemoteInfo in config.Remotes
|
||||
func getRemotes(cmd *cobra.Command) []remote.Remote {
|
||||
gitRemotes := make([]remote.Remote, 0)
|
||||
for _, gitea := range conf.Giteas {
|
||||
if gitea.CloneProto == "" {
|
||||
gitea.CloneProto = info.DefaultCloneProto
|
||||
for _, r := range conf.Remotes {
|
||||
r.Ctx = cmd.Context()
|
||||
var gitRemote remote.Remote
|
||||
var err error
|
||||
switch r.Type {
|
||||
case "gitlab":
|
||||
gitRemote, err = gitlabremote.NewGitlabRemote(&r)
|
||||
case "gitea":
|
||||
gitRemote, err = gitearemote.NewGiteaRemote(&r)
|
||||
}
|
||||
giteaRemote, err := gitearemote.NewGiteaRemote(&info.RemoteInfo{
|
||||
Ctx: cmd.Context(),
|
||||
Host: gitea.Host,
|
||||
Name: gitea.Name,
|
||||
Token: gitea.Token,
|
||||
Type: "gitea",
|
||||
CloneProto: gitea.CloneProto,
|
||||
})
|
||||
if err != nil {
|
||||
plog.Error("Failed to prepare Gitea remote", plog.Args("error", err))
|
||||
plog.Error("Failed to prepare remote", plog.Args(
|
||||
"error", err,
|
||||
"type", r.Type))
|
||||
} else {
|
||||
gitRemotes = append(gitRemotes, giteaRemote)
|
||||
gitRemotes = append(gitRemotes, gitRemote)
|
||||
}
|
||||
}
|
||||
return gitRemotes
|
||||
}
|
||||
|
||||
// Loads all configured gitlabs, including as defined by legacy
|
||||
// top-level keys
|
||||
func getGitLabRemotes(cmd *cobra.Command) []remote.Remote {
|
||||
gitRemotes := make([]remote.Remote, 0)
|
||||
|
||||
// Support legacy keys
|
||||
if conf.GitlabHost != "" && conf.GitlabToken != "" {
|
||||
conf.Gitlabs = append(conf.Gitlabs, config.GitlabConfig{
|
||||
Host: conf.GitlabHost,
|
||||
Name: conf.GitlabHost,
|
||||
Token: conf.GitlabToken,
|
||||
CloneProto: info.CloneProtoSSH,
|
||||
})
|
||||
}
|
||||
|
||||
// Load Gitlabs
|
||||
for _, gl := range conf.Gitlabs {
|
||||
if gl.CloneProto == "" {
|
||||
gl.CloneProto = info.DefaultCloneProto
|
||||
}
|
||||
gitlabRemote, err := gitlabremote.NewGitlabRemote(&info.RemoteInfo{
|
||||
Ctx: cmd.Context(),
|
||||
Host: gl.Host,
|
||||
Name: gl.Name,
|
||||
Token: gl.Token,
|
||||
Type: "gitlab",
|
||||
CloneProto: gl.CloneProto,
|
||||
})
|
||||
if err != nil {
|
||||
plog.Error("Failed to prepare GitLab remote", plog.Args("error", err))
|
||||
} else {
|
||||
gitRemotes = append(gitRemotes, gitlabRemote)
|
||||
}
|
||||
}
|
||||
|
||||
return gitRemotes
|
||||
}
|
||||
|
||||
func postProjectCache(cmd *cobra.Command, args []string) {
|
||||
projectCache.Write()
|
||||
}
|
||||
|
Reference in New Issue
Block a user