@@ -12,6 +12,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
12
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
See the License for the specific language governing permissions and
14
14
limitations under the License.
15
+
15
16
*/
16
17
package cmd
17
18
@@ -29,7 +30,7 @@ import (
29
30
"github.com/slashdevops/aws_cloudwatch_exporter/config"
30
31
"github.com/spf13/cobra"
31
32
"github.com/spf13/viper"
32
- "gopkg.in/yaml.v2 "
33
+ "gopkg.in/yaml.v3 "
33
34
)
34
35
35
36
const (
@@ -88,24 +89,6 @@ func init() {
88
89
}
89
90
90
91
// AWS Credentials conf
91
- // AccessKeyID
92
- rootCmd .PersistentFlags ().StringVar (& conf .AWS .AccessKeyID , "access_key_id" , "" , "The AWS AccessKeyID, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
93
- if err := viper .BindPFlag ("aws.access_key_id" , rootCmd .PersistentFlags ().Lookup ("access_key_id" )); err != nil {
94
- log .Error (err )
95
- }
96
-
97
- // SecretAccessKey
98
- rootCmd .PersistentFlags ().StringVar (& conf .AWS .SecretAccessKey , "secret_access_key" , "" , "The AWS SecretAccessKey, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
99
- if err := viper .BindPFlag ("aws.secret_access_key" , rootCmd .PersistentFlags ().Lookup ("secret_access_key" )); err != nil {
100
- log .Error (err )
101
- }
102
-
103
- // SessionToken
104
- rootCmd .PersistentFlags ().StringVar (& conf .AWS .SessionToken , "session_token" , "" , "The AWS SessionToken, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
105
- if err := viper .BindPFlag ("aws.session_token" , rootCmd .PersistentFlags ().Lookup ("session_token" )); err != nil {
106
- log .Error (err )
107
- }
108
-
109
92
// Region
110
93
rootCmd .PersistentFlags ().StringVar (& conf .AWS .Region , "region" , "" , "The AWS Region, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
111
94
if err := viper .BindPFlag ("aws.region" , rootCmd .PersistentFlags ().Lookup ("region" )); err != nil {
@@ -123,48 +106,6 @@ func init() {
123
106
if err := viper .BindPFlag ("aws.role_arn" , rootCmd .PersistentFlags ().Lookup ("role_arn" )); err != nil {
124
107
log .Error (err )
125
108
}
126
-
127
- // RoleSessionName
128
- rootCmd .PersistentFlags ().StringVar (& conf .AWS .RoleSessionName , "role_session_name" , "" , "The AWS RoleSessionName, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
129
- if err := viper .BindPFlag ("aws.role_session_name" , rootCmd .PersistentFlags ().Lookup ("role_session_name" )); err != nil {
130
- log .Error (err )
131
- }
132
-
133
- // WebIdentityTokenFile
134
- rootCmd .PersistentFlags ().StringVar (& conf .AWS .WebIdentityTokenFile , "web_identity_token_file" , "" , "The AWS WebIdentityTokenFile, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
135
- if err := viper .BindPFlag ("aws.web_identity_token_file" , rootCmd .PersistentFlags ().Lookup ("web_identity_token_file" )); err != nil {
136
- log .Error (err )
137
- }
138
-
139
- // ExternalID
140
- rootCmd .PersistentFlags ().StringVar (& conf .AWS .ExternalID , "external_id" , "" , "The AWS ExternalID, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
141
- if err := viper .BindPFlag ("aws.external_id" , rootCmd .PersistentFlags ().Lookup ("external_id" )); err != nil {
142
- log .Error (err )
143
- }
144
-
145
- // MFASerial
146
- rootCmd .PersistentFlags ().StringVar (& conf .AWS .MFASerial , "mfa_serial" , "" , "The AWS MFASerial, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
147
- if err := viper .BindPFlag ("aws.mfa_serial" , rootCmd .PersistentFlags ().Lookup ("mfa_serial" )); err != nil {
148
- log .Error (err )
149
- }
150
-
151
- // SharedConfigState
152
- rootCmd .PersistentFlags ().BoolVar (& conf .AWS .SharedConfigState , "shared_config_state" , true , "The AWS SharedConfigState, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" )
153
- if err := viper .BindPFlag ("aws.shared_config_state" , rootCmd .PersistentFlags ().Lookup ("shared_config_state" )); err != nil {
154
- log .Error (err )
155
- }
156
-
157
- // SharedCredentialsFile
158
- rootCmd .PersistentFlags ().StringSliceVar (& conf .AWS .SharedCredentialsFile , "shared_credential_file" , nil , "The AWS SharedCredentialsFile, example: --shared_credential_file ~/.aws/credentials --shared_credential_file /etc/aws/credentials" )
159
- if err := viper .BindPFlag ("aws.shared_credential_file" , rootCmd .PersistentFlags ().Lookup ("shared_credential_file" )); err != nil {
160
- log .Error (err )
161
- }
162
-
163
- // ConfigFile
164
- rootCmd .PersistentFlags ().StringSliceVar (& conf .AWS .ConfigFile , "config_file" , nil , "The AWS ConfigFile, example: --config_file ~/.aws/config --config_file /etc/aws/config" )
165
- if err := viper .BindPFlag ("aws.config_file" , rootCmd .PersistentFlags ().Lookup ("config_file" )); err != nil {
166
- log .Error (err )
167
- }
168
109
}
169
110
170
111
func initConfig () {
@@ -201,80 +142,43 @@ func initConfig() {
201
142
conf .Application .BuildInfo = version .BuildContext ()
202
143
}
203
144
204
- // this will be used for every commands that needs conf in files
205
- func ReadAndValidateConfFromFiles () {
206
- // Read env vars equals as the mapstructure defined into the config.go
207
- viper .AutomaticEnv ()
208
- viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
209
-
210
- loadFromConfigFiles (& conf )
211
- loadFromMetricsFiles (& conf )
212
- validateMetricsQueries (& conf )
145
+ // Unmarshall Yaml files into c config structure
146
+ func loadFromConfigFiles (fileName string , c * config.All ) {
213
147
214
- // expose all the configuration, just to check
215
- if conf .Server .Debug {
216
- log .Debug (conf .ToJSON ())
217
- // log.VersionInfo(conf.ToYAML())
148
+ if ! fileExists (fileName ) {
149
+ log .Warnf ("The file %s doesn't exist, I will try to use configuration values from flags or ENV vars" , fileName )
218
150
}
219
- }
220
151
221
- // this will be used for every commands that needs conf in files
222
- func ReadAndValidateMetricsFromFiles () {
152
+ log .Infof ("Reading configuration file: %s" , fileName )
153
+
154
+ // fileNameNoExt := strings.TrimSuffix(file, filepath.Ext(file))
155
+
156
+ log .Debugf ("Parsing configuration file path: %s" , fileName )
157
+ log .Debugf ("File: %s" , filepath .Base (fileName ))
158
+ // log.Debugf("File without ext: %s", fileNameNoExt)
159
+ log .Debugf ("Location: %s" , filepath .Dir (fileName ))
160
+ log .Debugf ("File ext: %s" , filepath .Ext (fileName )[1 :])
161
+
162
+ // viper.SetConfigName(fileNameNoExt)
163
+ viper .SetConfigName (filepath .Base (fileName ))
164
+ viper .AddConfigPath (filepath .Dir (fileName ))
165
+ viper .SetConfigType (filepath .Ext (fileName )[1 :])
166
+
223
167
// Read env vars equals as the mapstructure defined into the config.go
224
168
viper .AutomaticEnv ()
225
169
viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
226
170
227
- loadFromMetricsFiles (& conf )
228
- validateMetricsQueries (& conf )
229
-
230
- // expose all the configuration, just to check
231
- if conf .Server .Debug {
232
- log .Debug (conf .ToJSON ())
233
- // log.VersionInfo(conf.ToYAML())
171
+ log .Debugf ("Loading configuration from file: %s" , fileName )
172
+ if err := viper .ReadInConfig (); err != nil {
173
+ log .Fatalf ("Error reading config file, %s" , err )
234
174
}
235
- }
236
175
237
- // Unmarshall Yaml files into c config structure
238
- func loadFromConfigFiles (c * config.All ) {
239
- // Config files to be load
240
- files := []string {
241
- c .Application .ServerFile ,
242
- c .Application .CredentialsFile ,
176
+ log .Debugf ("Filling configuration structure from file: %s" , fileName )
177
+ err := viper .Unmarshal (& c )
178
+ if err != nil {
179
+ log .Fatalf ("Unable to decode into struct, %v" , err )
243
180
}
244
181
245
- for _ , file := range files {
246
-
247
- if ! fileExists (file ) {
248
- log .Warnf ("The file %s doesn't exist, I will try to use configuration values from flags or ENV vars" , file )
249
- break
250
- }
251
-
252
- log .Infof ("Reading configuration file: %s" , file )
253
-
254
- // fileNameNoExt := strings.TrimSuffix(file, filepath.Ext(file))
255
-
256
- log .Debugf ("Parsing configuration file path: %s" , file )
257
- log .Debugf ("File: %s" , filepath .Base (file ))
258
- // log.Debugf("File without ext: %s", fileNameNoExt)
259
- log .Debugf ("Location: %s" , filepath .Dir (file ))
260
- log .Debugf ("File ext: %s" , filepath .Ext (file )[1 :])
261
-
262
- // viper.SetConfigName(fileNameNoExt)
263
- viper .SetConfigName (filepath .Base (file ))
264
- viper .AddConfigPath (filepath .Dir (file ))
265
- viper .SetConfigType (filepath .Ext (file )[1 :])
266
-
267
- log .Debugf ("Loading configuration from file: %s" , file )
268
- if err := viper .ReadInConfig (); err != nil {
269
- log .Fatalf ("Error reading config file, %s" , err )
270
- }
271
-
272
- log .Debugf ("Filling configuration structure from file: %s" , file )
273
- err := viper .Unmarshal (& c )
274
- if err != nil {
275
- log .Fatalf ("Unable to decode into struct, %v" , err )
276
- }
277
- }
278
182
}
279
183
280
184
// Unmarshall Yaml files into c config structure
0 commit comments