Skip to content

Commit

Permalink
Merge pull request #468 from nokia/expand-globals-env
Browse files Browse the repository at this point in the history
properly expand global attributes if set to an environment variable
  • Loading branch information
karimra committed Jun 20, 2024
2 parents a9bedd0 + 85d658a commit 5f1fdc5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func (a *App) InitGlobalFlags() {
}

func (a *App) PreRunE(cmd *cobra.Command, args []string) error {
a.Config.SetGlobalsFromEnv(a.RootCmd)
a.Config.SetPersistentFlagsFromFile(a.RootCmd)

logOutput, flags, err := a.Config.SetLogger()
Expand Down
11 changes: 11 additions & 0 deletions pkg/config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ package config
import (
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func envToMap() map[string]interface{} {
Expand Down Expand Up @@ -54,6 +57,14 @@ func (c *Config) mergeEnvVars() {
c.FileConfig.MergeConfigMap(envs)
}

func (c *Config) SetGlobalsFromEnv(cmd *cobra.Command) {
cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && c.FileConfig.IsSet(f.Name) {
c.setFlagValue(cmd, f.Name, os.ExpandEnv(c.FileConfig.GetString(f.Name)))
}
})
}

func expandMapEnv(m map[string]interface{}, except ...string) {
OUTER:
for f := range m {
Expand Down

0 comments on commit 5f1fdc5

Please sign in to comment.