Skip to content

Commit 7fa5e7d

Browse files
authored
fix(cli): error on missing config file (#7154)
1 parent 8c87194 commit 7fa5e7d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pkg/commands/app.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ func loadPluginCommands() []*cobra.Command {
151151
return commands
152152
}
153153

154-
func initConfig(configFile string) error {
154+
func initConfig(configFile string, pathChanged bool) error {
155155
// Read from config
156156
viper.SetConfigFile(configFile)
157157
viper.SetConfigType("yaml")
158158
if err := viper.ReadInConfig(); err != nil {
159159
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+
}
162164
}
163165
return xerrors.Errorf("config file %q loading error: %s", configFile, err)
164166
}
@@ -201,7 +203,7 @@ func NewRootCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
201203

202204
// Configure environment variables and config file
203205
// 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 {
205207
return err
206208
}
207209

pkg/commands/app_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ func TestFlags(t *testing.T) {
296296
},
297297
wantErr: `invalid argument "foo" for "--format" flag`,
298298
},
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+
},
299308
}
300309

301310
for _, tt := range tests {

0 commit comments

Comments
 (0)