@@ -10,6 +10,7 @@ import (
10
10
"silabs/get-zap/jf"
11
11
12
12
"github.com/spf13/cobra"
13
+ "github.com/spf13/pflag"
13
14
"github.com/spf13/viper"
14
15
)
15
16
@@ -28,8 +29,6 @@ const useRt = "useRt"
28
29
const useGh = "useGh"
29
30
const localRoot = "localRoot"
30
31
31
- var cfgFile string
32
-
33
32
// rootCmd represents the base command when called without any subcommands
34
33
var rootCmd = & cobra.Command {
35
34
Use : "get-zap" ,
@@ -70,9 +69,9 @@ func Execute() {
70
69
}
71
70
72
71
func init () {
73
- cobra .OnInitialize (initConfig )
72
+ cobra .OnInitialize (initViper )
74
73
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 ) )
76
75
rootCmd .PersistentFlags ().String (ownerArg , "project-chip" , "Owner of the github repository." )
77
76
rootCmd .PersistentFlags ().String (repoArg , "zap" , "Name of the github repository." )
78
77
rootCmd .PersistentFlags ().StringP (githubTokenArg , "t" , "" , "Github token to use for authentication." )
@@ -86,42 +85,36 @@ func init() {
86
85
rootCmd .PersistentFlags ().String (rtPath , "" , "Artifactory path within the repo." )
87
86
rootCmd .PersistentFlags ().Bool (useRt , true , "Use Artifactory." )
88
87
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 ))
103
88
}
104
89
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 )
109
99
} 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.
118
103
}
119
104
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 ()
122
108
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 ())
127
118
}
119
+
120
+ // EO Viper configuration.
0 commit comments