Move from --gitlab flags to global --remote flags

This commit is contained in:
2024-01-17 10:06:20 -05:00
parent f33199bd7b
commit 702f599f5f
19 changed files with 152 additions and 121 deletions

View File

@@ -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)