Skip to content

Commit

Permalink
Refactor admxgen to follow our other CLIs
Browse files Browse the repository at this point in the history
Remove environment prefix since we never used it and it's not documented
anywhere, and use decorate to bind the viper flags.
  • Loading branch information
GabrielNagy committed May 29, 2024
1 parent 6ab8ec4 commit bb73f60
Showing 1 changed file with 15 additions and 59 deletions.
74 changes: 15 additions & 59 deletions cmd/admxgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@
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,
DisableTimestamp: true,
})

viper := viper.New()
viper.SetEnvPrefix(viperPrefix)

rootCmd := cobra.Command{
Use: "admxgen COMMAND",
Expand All @@ -43,32 +37,20 @@ 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)
os.Exit(1)
}
}

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"),
Expand All @@ -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",
Expand All @@ -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"),
Expand All @@ -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
}

0 comments on commit bb73f60

Please sign in to comment.