Last active
August 25, 2016 13:32
-
-
Save jayunit100/4d206f17aff489fe83bb83a29915881d to your computer and use it in GitHub Desktop.
viper gist hacking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enable viper configuration management. This is required to | |
// 1) Feature: Allow files/etcd/other dynamic configuration setup. | |
// 2) Fix a Bug: Avoid transitive dependencies on flag. | |
func ViperizeAndCreateMockFlags() { | |
fmt.Println( | |
"flags: ", | |
"\n pflag:",flag.Args(), | |
"\n goflag:",goflag.Args(), | |
" rem:",flag.NArg()," ",goflag.NArg()) | |
// TODO @jayunit100: Viperize all of this stuff. | |
RegisterCommonFlags() | |
RegisterClusterFlags() | |
//FIRST: Prevent flag from exploding due to unknown flags... | |
mockFlagBinder := func(f *flag.Flag) { | |
if f.Value.Type() == "string" { | |
fmt.Println("[go flag] Binding flag ", f.Name , f.Value) | |
goflag.String(f.Name, f.Value.String(), f.Usage) | |
} else { | |
fmt.Println("[goflag] Ignoring an unknown type ", f.Value.Type()) | |
} | |
} | |
E2eflags.VisitAll(mockFlagBinder) | |
// Try to catch goflag errors early on, so that we dont have to trace failures in transitive deps. | |
fmt.Println("--> goflag parse test.") | |
goflag.Parse() | |
fmt.Println("<-- with a goflag parse test.") | |
// Add Viper support(i.e. make sure users can add an e2e.json file that is read in). | |
viper.SetConfigName("e2e") | |
viper.AddConfigPath(".") | |
// viperFunc := func(f *flag.Flag) { | |
// fmt.Println("Viper: binding ",f.Name) | |
// viper.BindPFlag(f.Name, f) | |
// } | |
// TODO redundant flag binding? possibly delete the first one. | |
// framework.E2eflags.VisitAll(viperFunc) | |
// viper.BindPFlags(E2eflags) | |
err := viper.ReadInConfig() | |
if err != nil { | |
glog.Warningf("Error related to viper configuration: %v. To avoid this, create an e2e.json file or similar.", err) | |
} | |
// 4) Overwrite with viper values where available. | |
viperFlagSetter := func(f *flag.Flag) { | |
if viper.IsSet(f.Name) { | |
fmt.Println("[overwrite] viper found a Settting for ",f.Name, " ",f.Value) | |
f.Value.Set(viper.GetString(f.Name)) | |
} else { | |
fmt.Println("[overwrite] need to add support for ", f.Value.Type(), " -- ", f.Name) | |
} | |
} | |
// First add viper . . . | |
E2eflags.Visit(viperFlagSetter) | |
// Now add others . . . | |
flag.CommandLine.AddGoFlagSet(goflag.CommandLine) | |
error := flag.CommandLine.Parse(os.Args[1:]) | |
if err != nil { | |
fmt.Println(error) | |
panic(error) | |
} | |
fmt.Println("os: ", os.Args, "flags: \n pflag:",flag.Args(),"\n goflag:",goflag.Args()," ") | |
fmt.Println("Viper: ", viper.Get("provider"), " TestContext.Provider:",TestContext.Provider) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment