Skip to content

Commit 952286d

Browse files
authored
Parse nested maps from viper configuration (#639)
* Parse nested maps from viper configuration * Apply suggestions from code review
1 parent 142cd5b commit 952286d

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

cmd/create/create.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ func New(cloud *common.Cloud) *cobra.Command {
6262
func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) error {
6363
variables := make(map[string]*string)
6464
for name, value := range o.Environment {
65+
name = strings.ToUpper(name)
6566
variables[name] = nil
66-
if value != "" {
67-
variables[name] = &value
67+
if copy := value; value != "" {
68+
variables[name] = &copy
6869
}
6970
}
7071

cmd/root.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
56
"strings"
67
"time"
@@ -143,20 +144,24 @@ func New() *cobra.Command {
143144
}
144145

145146
for _, cmd := range append(cmd.Commands(), cmd) {
146-
cobra.CheckErr(viper.BindPFlags(cmd.Flags()))
147-
cobra.CheckErr(viper.BindPFlags(cmd.PersistentFlags()))
148-
149-
cmd.Flags().VisitAll(func(f *pflag.Flag) {
150-
if val := viper.GetString(f.Name); viper.IsSet(f.Name) && val != "" {
151-
cobra.CheckErr(cmd.Flags().Set(f.Name, val))
152-
}
153-
})
154-
155-
cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
156-
if val := viper.GetString(f.Name); viper.IsSet(f.Name) && val != "" {
157-
cobra.CheckErr(cmd.PersistentFlags().Set(f.Name, val))
158-
}
159-
})
147+
for _, flagSet := range []*pflag.FlagSet{
148+
cmd.PersistentFlags(),
149+
cmd.Flags(),
150+
} {
151+
cobra.CheckErr(viper.BindPFlags(flagSet))
152+
flagSet.VisitAll(func(f *pflag.Flag) {
153+
if viper.IsSet(f.Name) {
154+
switch val := viper.Get(f.Name).(type) {
155+
case map[string]interface{}:
156+
for k, v := range val {
157+
cobra.CheckErr(flagSet.Set(f.Name, fmt.Sprintf("%s=%s", k, v)))
158+
}
159+
default:
160+
cobra.CheckErr(flagSet.Set(f.Name, viper.GetString(f.Name)))
161+
}
162+
}
163+
})
164+
}
160165
}
161166

162167
return cmd

0 commit comments

Comments
 (0)