Skip to content

Commit

Permalink
Enable CLI flags to be mapped to env vars:
Browse files Browse the repository at this point in the history
This enables passing env vars instead of cli flags.
Will be used for BMC secrets.
  • Loading branch information
jacobweinstock committed Sep 28, 2023
1 parent 8f7e490 commit 02d6225
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cmd/eksctl-anywhere/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -25,14 +26,24 @@ https://anywhere.eks.amazonaws.com/docs/troubleshooting/troubleshooting/#bootstr
)

func bindFlagsToViper(cmd *cobra.Command, args []string) error {
var err error
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
if err != nil {
if err := viper.BindPFlag(flag.Name, flag); err != nil {
return
}
err = viper.BindPFlag(flag.Name, flag)
viper.AutomaticEnv()
// Environment variables can't have dashes in them, so bind them to their equivalent
// keys with underscores, e.g. --hardware-csv to HARDWARE_CSV
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// viper.AutomaticEnv() needs help with dashes in flag names.
if !flag.Changed && viper.IsSet(flag.Name) {
val := viper.Get(flag.Name)
if err := cmd.Flags().Set(flag.Name, fmt.Sprintf("%v", val)); err != nil {
return
}
}
})
return err

return nil
}

func applyClusterOptionFlags(flagSet *pflag.FlagSet, clusterOpt *clusterOptions) {
Expand Down

0 comments on commit 02d6225

Please sign in to comment.