Skip to content

Commit

Permalink
Use prefix for environment variables
Browse files Browse the repository at this point in the history
Avoids clashing with existing variables

Signed-off-by: Chris <github.account@chrigel.net>
  • Loading branch information
ccremer committed Jul 3, 2020
1 parent 6e739d6 commit 289dde1
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions cmd/root.go
Expand Up @@ -21,8 +21,8 @@ var (
Short: "Keeps your Kubernetes projects clean",
PersistentPreRun: parseConfig,
}
config = cfg.NewDefaultConfig()
k = koanf.New(".")
config = cfg.NewDefaultConfig()
koanfInstance = koanf.New(".")
)

// Execute is the main entrypoint of the CLI, it executes child commands as given by the user-defined flags and arguments.
Expand All @@ -45,19 +45,10 @@ func initRootConfig() {
// parseConfig reads the flags and ENV vars
func parseConfig(cmd *cobra.Command, args []string) {

err := k.Load(env.Provider("", ".", func(s string) string {
// First replace the double underscore with . as the hierarchy delimiter
s = strings.Replace(strings.ToLower(s), "__", ".", -1)
// Now replace the remaining single underscore with dashes to allow flags with dashes in it.
return strings.Replace(s, "_", "-", -1)
}), nil)
if err != nil {
log.WithError(err).Fatal("Could not load environment variables")
}

loadEnvironmentVariables()
bindFlags(cmd.PersistentFlags())

if err := k.Unmarshal("", &config); err != nil {
if err := koanfInstance.Unmarshal("", &config); err != nil {
log.WithError(err).Fatal("Could not read config")
}

Expand All @@ -82,8 +73,26 @@ func parseConfig(cmd *cobra.Command, args []string) {
}
}

func loadEnvironmentVariables() {
prefix := "SEISO_"
err := koanfInstance.Load(env.Provider(prefix, ".", func(s string) string {
/*
Configuration can contain hierarchies (YAML, etc.) and CLI flags dashes. To read environment variables with
hierarchies and dashes we replace the hierarchy delimiter with double underscore and dashes with single underscore,
so that parent.child-with-dash becomes PARENT__CHILD_WITH_DASH
*/
s = strings.TrimPrefix(s, prefix)
s = strings.Replace(strings.ToLower(s), "__", ".", -1)
s = strings.Replace(strings.ToLower(s), "_", "-", -1)
return s
}), nil)
if err != nil {
log.WithError(err).Fatal("Could not load environment variables")
}
}

func bindFlags(flagSet *pflag.FlagSet) {
err := k.Load(posflag.Provider(flagSet, ".", k), nil)
err := koanfInstance.Load(posflag.Provider(flagSet, ".", koanfInstance), nil)
if err != nil {
log.WithError(err).Fatal("Could not bind flags")
}
Expand Down

0 comments on commit 289dde1

Please sign in to comment.