Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fix merge flag default value in config #1246

Merged
merged 1 commit into from
Apr 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 40 additions & 1 deletion daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"sync"

Expand Down Expand Up @@ -158,11 +159,49 @@ func (cfg *Config) MergeConfigurations(config *Config, flagSet *pflag.FlagSet) e
}

// merge configurations from command line flags and config file
err = mergeConfigurations(fileConfig, cfg)
err = mergeConfigurations(fileConfig, cfg.delValue(flagSet, fileFlags))
return err

}

// delValue deleles value in config, since we do not do conflict check for slice
// type flag, note that we should remove default flag value in merging, cause
// this is not reasonable if the flag is not passed. Just set the flag value to
// null, when same flag has been set in config file.
func (cfg *Config) delValue(flagSet *pflag.FlagSet, fileFlags map[string]interface{}) *Config {
flagSet.VisitAll(func(f *pflag.Flag) {
// if flag type not slice or array , then skip
if !strings.Contains(f.Value.Type(), "Slice") && !strings.Contains(f.Value.Type(), "Array") {
return
}

// if flag is set in command line, then skip
if f.Changed {
return
}

// if flag is not set in config file, then skip
if _, exist := fileFlags[f.Name]; !exist {
return
}

// set value as null in config
r := reflect.ValueOf(cfg).Elem()
rtype := r.Type()
for i := 0; i < r.NumField(); i++ {
if rtype.Field(i).Type.Kind() != reflect.Slice {
continue
}
if strings.Contains(rtype.Field(i).Tag.Get("json"), f.Name) {
r.Field(i).Set(reflect.MakeSlice(reflect.TypeOf([]string{}), 0, 0))
}
}

})

return cfg
}

// iterateConfig resolves key-value from config file iteratly.
func iterateConfig(origin map[string]interface{}, config map[string]interface{}) {
for k, v := range origin {
Expand Down