File tree Expand file tree Collapse file tree 2 files changed +15
-4
lines changed Expand file tree Collapse file tree 2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -151,14 +151,16 @@ func loadPluginCommands() []*cobra.Command {
151
151
return commands
152
152
}
153
153
154
- func initConfig (configFile string ) error {
154
+ func initConfig (configFile string , pathChanged bool ) error {
155
155
// Read from config
156
156
viper .SetConfigFile (configFile )
157
157
viper .SetConfigType ("yaml" )
158
158
if err := viper .ReadInConfig (); err != nil {
159
159
if errors .Is (err , os .ErrNotExist ) {
160
- log .Debug ("Config file not found" , log .FilePath (configFile ))
161
- return nil
160
+ if ! pathChanged {
161
+ log .Debugf ("Default config file %q not found, using built in values" , log .FilePath (configFile ))
162
+ return nil
163
+ }
162
164
}
163
165
return xerrors .Errorf ("config file %q loading error: %s" , configFile , err )
164
166
}
@@ -201,7 +203,7 @@ func NewRootCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
201
203
202
204
// Configure environment variables and config file
203
205
// It cannot be called in init() because it must be called after viper.BindPFlags.
204
- if err := initConfig (configPath ); err != nil {
206
+ if err := initConfig (configPath , cmd . Flags (). Changed ( flag . ConfigFileFlag . ConfigName ) ); err != nil {
205
207
return err
206
208
}
207
209
Original file line number Diff line number Diff line change @@ -296,6 +296,15 @@ func TestFlags(t *testing.T) {
296
296
},
297
297
wantErr : `invalid argument "foo" for "--format" flag` ,
298
298
},
299
+ {
300
+ name : "missing config file" ,
301
+ arguments : []string {
302
+ "test" ,
303
+ "--config" ,
304
+ "none" ,
305
+ },
306
+ wantErr : `config file "none" loading error: open none:` ,
307
+ },
299
308
}
300
309
301
310
for _ , tt := range tests {
You can’t perform that action at this time.
0 commit comments