From bb73f60b408c1d23d4803a18a1f533a848bd6bb4 Mon Sep 17 00:00:00 2001 From: Gabriel Nagy Date: Wed, 29 May 2024 18:52:15 +0300 Subject: [PATCH] Refactor admxgen to follow our other CLIs Remove environment prefix since we never used it and it's not documented anywhere, and use decorate to bind the viper flags. --- cmd/admxgen/main.go | 74 +++++++++------------------------------------ 1 file changed, 15 insertions(+), 59 deletions(-) diff --git a/cmd/admxgen/main.go b/cmd/admxgen/main.go index 016f82cb8..0c170388b 100644 --- a/cmd/admxgen/main.go +++ b/cmd/admxgen/main.go @@ -4,23 +4,18 @@ package main import ( - "errors" - "fmt" "os" - "strings" "github.com/leonelquinteros/gotext" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/spf13/viper" "github.com/ubuntu/adsys/internal/ad/admxgen" "github.com/ubuntu/adsys/internal/cmdhandler" "github.com/ubuntu/adsys/internal/config" + "github.com/ubuntu/decorate" ) -const viperPrefix = "ADMXGEN" - func main() { log.SetFormatter(&log.TextFormatter{ DisableLevelTruncation: true, @@ -28,7 +23,6 @@ func main() { }) viper := viper.New() - viper.SetEnvPrefix(viperPrefix) rootCmd := cobra.Command{ Use: "admxgen COMMAND", @@ -43,24 +37,12 @@ func main() { } rootCmd.PersistentFlags().CountP("verbose", "v", gotext.Get("issue INFO (-v), DEBUG (-vv) or DEBUG with caller (-vvv) output")) - if err := bindFlags(viper, rootCmd.PersistentFlags()); err != nil { - log.Error(gotext.Get("can't install command flag bindings: %v", err)) - os.Exit(2) - } + decorate.LogOnError(viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))) // Install subcommands - if err := installExpand(&rootCmd, viper); err != nil { - log.Error(err) - os.Exit(2) - } - if err := installAdmx(&rootCmd, viper); err != nil { - log.Error(err) - os.Exit(2) - } - if err := installDoc(&rootCmd, viper); err != nil { - log.Error(err) - os.Exit(2) - } + installExpand(&rootCmd, viper) + installAdmx(&rootCmd, viper) + installDoc(&rootCmd, viper) if err := rootCmd.Execute(); err != nil { log.Error(err) @@ -68,7 +50,7 @@ func main() { } } -func installExpand(rootCmd *cobra.Command, viper *viper.Viper) error { +func installExpand(rootCmd *cobra.Command, viper *viper.Viper) { cmd := &cobra.Command{ Use: "expand SOURCE DEST", Short: gotext.Get("Generates intermediary policy definition files"), @@ -80,17 +62,15 @@ The generated definition file will be of the form expanded_policies.RELEASE.yaml }, } cmd.Flags().StringP("root", "r", "/", gotext.Get("root filesystem path to use. Default to /.")) - cmd.Flags().StringP("current-session", "s", "", gotext.Get(`current session to consider for dconf per-session - overrides. Default to "".`)) - if err := bindFlags(viper, cmd.Flags()); err != nil { - return errors.New(gotext.Get("can't install command flag bindings: %v", err)) - } + decorate.LogOnError(viper.BindPFlag("root", cmd.Flags().Lookup("root"))) + + cmd.Flags().StringP("current-session", "s", "", gotext.Get(`current session to consider for dconf per-session overrides. Default to "".`)) + decorate.LogOnError(viper.BindPFlag("current-session", cmd.Flags().Lookup("current-session"))) rootCmd.AddCommand(cmd) - return nil } -func installAdmx(rootCmd *cobra.Command, viper *viper.Viper) error { +func installAdmx(rootCmd *cobra.Command, viper *viper.Viper) { var autoDetectReleases, allowMissingKeys *bool cmd := &cobra.Command{ Use: "admx CATEGORIES_DEF.YAML SOURCE DEST", @@ -102,16 +82,15 @@ func installAdmx(rootCmd *cobra.Command, viper *viper.Viper) error { }, } autoDetectReleases = cmd.Flags().BoolP("auto-detect-releases", "a", false, gotext.Get("override supported releases in categories definition file and will takes all yaml files in SOURCE directory and use the basename as their versions.")) + decorate.LogOnError(viper.BindPFlag("auto-detect-releases", cmd.Flags().Lookup("auto-detect-releases"))) + allowMissingKeys = cmd.Flags().BoolP("allow-missing-keys", "k", false, gotext.Get(`avoid fail but display a warning if some keys are not available in a release. This is the case when news keys are added to non-lts releases.`)) - if err := bindFlags(viper, cmd.Flags()); err != nil { - return errors.New(gotext.Get("can't install command flag bindings: %v", err)) - } + decorate.LogOnError(viper.BindPFlag("allow-missing-keys", cmd.Flags().Lookup("allow-missing-keys"))) rootCmd.AddCommand(cmd) - return nil } -func installDoc(rootCmd *cobra.Command, viper *viper.Viper) error { +func installDoc(rootCmd *cobra.Command, _ *viper.Viper) { cmd := &cobra.Command{ Use: "doc CATEGORIES_DEF.YAML SOURCE DEST", Short: gotext.Get("Create markdown documentation"), @@ -121,29 +100,6 @@ func installDoc(rootCmd *cobra.Command, viper *viper.Viper) error { return admxgen.GenerateDoc(args[0], args[1], args[2]) }, } - if err := bindFlags(viper, cmd.Flags()); err != nil { - return errors.New(gotext.Get("can't install command flag bindings: %v", err)) - } rootCmd.AddCommand(cmd) - return nil -} - -// bindFlags each cobra flag in a flagset to its associated viper env, ignoring config -// Compare to the viper automated binding, it translates - to _. -func bindFlags(viper *viper.Viper, flags *pflag.FlagSet) (errBind error) { - flags.VisitAll(func(f *pflag.Flag) { - // This allows to connect flag and default value to viper. - if err := viper.BindPFlag(f.Name, f); err != nil { - errBind = err - return - } - // And this is the env with translation from - to _. - envVar := strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_")) - if err := viper.BindEnv(f.Name, fmt.Sprintf("%s_%s", viperPrefix, envVar)); err != nil { - errBind = err - return - } - }) - return errBind }