Fix bugs, set constants

This commit is contained in:
2024-10-01 14:29:14 -04:00
parent a54f0629be
commit 03fcbe41ae
20 changed files with 165 additions and 89 deletions

View File

@@ -9,11 +9,14 @@ import (
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
)
var conf config.Config
var plog *pterm.Logger
var (
conf config.Config
plog *pterm.Logger
)
var rootCmd = &cobra.Command{
Use: "gitlab-project-manager",
@@ -36,7 +39,6 @@ func Execute() {
defer cncl()
err := rootCmd.ExecuteContext(ctx)
if err != nil {
pterm.Error.Printfln(pterm.LightYellow("Command failed, " + err.Error()))
os.Exit(1)
@@ -48,25 +50,25 @@ func init() {
cobra.OnInitialize(initConfig)
// Global flags
rootCmd.PersistentFlags().String("config", "",
rootCmd.PersistentFlags().String(FlagConfig, "",
"config file (default is "+defConfigPath+")")
rootCmd.PersistentFlags().String("projectPath", "",
rootCmd.PersistentFlags().String(FlagPath, "",
"Sets a path for local clones of projects")
rootCmd.PersistentFlags().String("logLevel", defLogLevel,
rootCmd.PersistentFlags().String(FlagLogLevel, defLogLevel,
"Default log level -- info, warn, error, debug")
rootCmd.PersistentFlags().StringSlice("remote", []string{},
rootCmd.PersistentFlags().StringSlice(FlagRemote, []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)
rootCmd.RegisterFlagCompletionFunc(FlagLogLevel, validLogLevelsFunc)
rootCmd.RegisterFlagCompletionFunc(FlagRemote, validRemotesFunc)
viper.BindPFlags(rootCmd.PersistentFlags())
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
cfgFile := viper.GetString("config")
cfgFile := viper.GetString(FlagConfig)
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
@@ -86,7 +88,7 @@ func initConfig() {
// Configure pretty logger
plog = pterm.DefaultLogger.
WithLevel(getPtermLogLevel(viper.GetString("logLevel"))).
WithLevel(getPtermLogLevel(viper.GetString(FlagLogLevel))).
WithWriter(os.Stderr)
if plog.Level == pterm.LogLevelDebug {
pterm.EnableDebugMessages()
@@ -137,7 +139,7 @@ func checkConfigPerms(file string) {
plog.Error("Failure reading configuration", plog.Args("err", err))
return
}
if stat.Mode().Perm()&0004 == 0004 {
if stat.Mode().Perm()&0o004 == 0o004 {
plog.Error("Configuration is world-readable. Recomment 0400.",
plog.Args("mode", stat.Mode().String()))
os.Exit(1)