Add feature with config file, flags, and env order

This commit is contained in:
Julien SENON 2018-03-21 01:05:55 +01:00
parent 9c9a65c53e
commit 6425032795
No known key found for this signature in database
GPG Key ID: 4FCF4DA1A7F57F3B
2 changed files with 41 additions and 29 deletions

View File

@ -21,7 +21,6 @@ import (
"fmt"
"io"
"os"
"strings"
"github.com/golang/glog"
"github.com/spf13/cobra"
@ -120,13 +119,12 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
}
})
cmd.PersistentFlags().StringVar(&rootCommand.configFile, "config", "", "config file (default is $HOME/.kops.yaml)")
cmd.PersistentFlags().StringVar(&rootCommand.configFile, "config", "", "yaml config file (default is $HOME/.kops.yaml)")
viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
viper.SetDefault("config", "$HOME/.kops.yaml")
defaultStateStore := os.Getenv("KOPS_STATE_STORE")
if strings.HasSuffix(defaultStateStore, "/") {
defaultStateStore = strings.TrimSuffix(defaultStateStore, "/")
}
cmd.PersistentFlags().StringVarP(&rootCommand.RegistryPath, "state", "", defaultStateStore, "Location of state storage. Overrides KOPS_STATE_STORE environment variable")
cmd.PersistentFlags().StringVar(&rootCommand.RegistryPath, "state", "", "Location of state storage. Overrides KOPS_STATE_STORE environment variable")
viper.BindPFlag("KOPS_STATE_STORE", cmd.PersistentFlags().Lookup("state"))
defaultClusterName := os.Getenv("KOPS_CLUSTER_NAME")
cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", defaultClusterName, "Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable")
@ -150,32 +148,46 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
// initConfig reads in config file and ENV variables if set.
func initConfig() {
// Take in order Flag --state, ENV, .kops.yaml, .kops/config
if os.Getenv("KOPS_STATE_STORE") != "" && rootCommand.RegistryPath == "" {
rootCommand.RegistryPath = os.Getenv("KOPS_STATE_STORE")
viper.BindEnv("KOPS_STATE_STORE")
fmt.Println("From env export, s3 :", rootCommand.RegistryPath)
viper.AutomaticEnv()
}
if rootCommand.configFile != "" {
// enable ability to specify config file via flag
viper.SetConfigFile(rootCommand.configFile)
}
viper.SetConfigName(".kops") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
if rootCommand.RegistryPath != "" {
viper.SetConfigFile(rootCommand.RegistryPath)
// If a state file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using state file:", viper.ConfigFileUsed())
viper.BindEnv("KOPS_STATE_STORE")
rootCommand.RegistryPath = viper.GetString("KOPS_STATE_STORE")
fmt.Println("Read Default config File, s3 :", rootCommand.RegistryPath)
viper.AutomaticEnv()
}
}
if rootCommand.configFile == "" && rootCommand.RegistryPath == "" {
viper.SetConfigName(".kops") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.ReadInConfig()
if err := viper.ReadInConfig(); err == nil {
viper.BindEnv("KOPS_STATE_STORE")
rootCommand.RegistryPath = viper.GetString("KOPS_STATE_STORE")
fmt.Println("Read .kops File, s3 :", rootCommand.RegistryPath)
viper.AutomaticEnv()
}
viper.SetConfigName("config")
viper.AddConfigPath("$HOME/.kops") // adding home/.kops/config directory as second search path
viper.ReadInConfig()
if err := viper.ReadInConfig(); err == nil {
viper.BindEnv("KOPS_STATE_STORE")
rootCommand.RegistryPath = viper.GetString("KOPS_STATE_STORE")
fmt.Println("Read .kops/config File, s3 :", rootCommand.RegistryPath)
viper.AutomaticEnv()
}
}
viper.BindEnv("KOPS_STATE_STORE", "KOPS_STATE_STORE")
// fmt.Println("Env state:", viper.GetString("KOPS_STATE_STORE"))
}
func (c *RootCmd) AddCommand(cmd *cobra.Command) {

View File

@ -22,7 +22,6 @@ import (
"strings"
"github.com/golang/glog"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/rest"
gceacls "k8s.io/kops/pkg/acls/gce"
@ -65,7 +64,8 @@ Trailing slash will be trimmed.`
func (f *Factory) Clientset() (simple.Clientset, error) {
if f.clientset == nil {
registryPath := viper.GetString("KOPS_STATE_STORE")
registryPath := f.options.RegistryPath
fmt.Println("Config S3:", registryPath)
if registryPath == "" {
return nil, field.Required(field.NewPath("State Store"), STATE_ERROR)
}