Skip to content

Commit 1917e12

Browse files
authored
Modify how viper is hooked up to cobra. (#10)
1 parent 34ee8e4 commit 1917e12

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

cmd/root.go

+29-36
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"silabs/get-zap/jf"
1111

1212
"github.com/spf13/cobra"
13+
"github.com/spf13/pflag"
1314
"github.com/spf13/viper"
1415
)
1516

@@ -28,8 +29,6 @@ const useRt = "useRt"
2829
const useGh = "useGh"
2930
const localRoot = "localRoot"
3031

31-
var cfgFile string
32-
3332
// rootCmd represents the base command when called without any subcommands
3433
var rootCmd = &cobra.Command{
3534
Use: "get-zap",
@@ -70,9 +69,9 @@ func Execute() {
7069
}
7170

7271
func init() {
73-
cobra.OnInitialize(initConfig)
72+
cobra.OnInitialize(initViper)
7473

75-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.get-zap.yaml)")
74+
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", fmt.Sprintf("config file (default is $HOME/.%s.json)", configKey))
7675
rootCmd.PersistentFlags().String(ownerArg, "project-chip", "Owner of the github repository.")
7776
rootCmd.PersistentFlags().String(repoArg, "zap", "Name of the github repository.")
7877
rootCmd.PersistentFlags().StringP(githubTokenArg, "t", "", "Github token to use for authentication.")
@@ -86,42 +85,36 @@ func init() {
8685
rootCmd.PersistentFlags().String(rtPath, "", "Artifactory path within the repo.")
8786
rootCmd.PersistentFlags().Bool(useRt, true, "Use Artifactory.")
8887
rootCmd.PersistentFlags().Bool(useGh, true, "Use GitHub.")
89-
90-
viper.BindPFlag(ownerArg, rootCmd.PersistentFlags().Lookup(ownerArg))
91-
viper.BindPFlag(repoArg, rootCmd.PersistentFlags().Lookup(repoArg))
92-
viper.BindPFlag(githubTokenArg, rootCmd.PersistentFlags().Lookup(githubTokenArg))
93-
viper.BindPFlag(releaseArg, rootCmd.PersistentFlags().Lookup(releaseArg))
94-
viper.BindPFlag(assetArg, rootCmd.PersistentFlags().Lookup(assetArg))
95-
viper.BindPFlag(rtUrl, rootCmd.PersistentFlags().Lookup(rtUrl))
96-
viper.BindPFlag(rtApiKey, rootCmd.PersistentFlags().Lookup(rtApiKey))
97-
viper.BindPFlag(rtUser, rootCmd.PersistentFlags().Lookup(rtUser))
98-
viper.BindPFlag(rtRepo, rootCmd.PersistentFlags().Lookup(rtRepo))
99-
viper.BindPFlag(rtPath, rootCmd.PersistentFlags().Lookup(rtPath))
100-
viper.BindPFlag(useRt, rootCmd.PersistentFlags().Lookup(useRt))
101-
viper.BindPFlag(useGh, rootCmd.PersistentFlags().Lookup(useGh))
102-
viper.BindPFlag(localRoot, rootCmd.PersistentFlags().Lookup(localRoot))
10388
}
10489

105-
func initConfig() {
106-
if cfgFile != "" {
107-
// Use config file from the flag.
108-
viper.SetConfigFile(cfgFile)
90+
// This function initializes viper, which is used to read configuration from environment variables and config files.
91+
const configKey = "get_zap"
92+
93+
var configFile = ""
94+
95+
func initViper() {
96+
97+
if configFile != "" {
98+
viper.SetConfigFile(configFile)
10999
} else {
110-
// Find home directory.
111-
home, err := os.UserHomeDir()
112-
cobra.CheckErr(err)
113-
114-
// Search config in home directory with name ".get-zap" (without extension).
115-
viper.AddConfigPath(home)
116-
viper.SetConfigType("json")
117-
viper.SetConfigName(".get-zap")
100+
viper.SetConfigName("." + configKey) // The config file is the configKey, prepended with a dot
101+
viper.AddConfigPath("$HOME") // Look in user home directory
102+
viper.SetConfigType("json") // And the file is in JSON format.
118103
}
119104

120-
viper.SetEnvPrefix("get_zap") // will be uppercased automatically
121-
viper.AutomaticEnv() // read in environment variables that match
105+
viper.ReadInConfig()
106+
viper.SetEnvPrefix(configKey)
107+
viper.AutomaticEnv()
122108

123-
// If a config file is found, read it in.
124-
if err := viper.ReadInConfig(); err == nil {
125-
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
126-
}
109+
// We bind all the flags, so that variables set by viper, but marked as required, are considered properly set
110+
rootCmd.Flags().VisitAll(func(f *pflag.Flag) {
111+
if !f.Changed && viper.IsSet(f.Name) {
112+
rootCmd.Flags().Set(f.Name, viper.GetString(f.Name))
113+
}
114+
})
115+
116+
viper.BindPFlags(rootCmd.PersistentFlags())
117+
viper.BindPFlags(rootCmd.Flags())
127118
}
119+
120+
// EO Viper configuration.

0 commit comments

Comments
 (0)