From 7e25efc9198c2946b30e41811b0f774cf719363c Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Fri, 12 Jul 2024 20:29:24 +0200 Subject: [PATCH 1/4] fix: error out when explicitly config file cannot be found The current behavior is to log a debug message and continue which means that the scan that will happen will not match the expectation of the user. Now, if a user explicitly set the config file path, an error will be raised otherwise, the original behavior will apply. --- pkg/commands/app.go | 10 ++++++---- pkg/commands/app_test.go | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/commands/app.go b/pkg/commands/app.go index 5ca651193c35..41c4ed78d509 100644 --- a/pkg/commands/app.go +++ b/pkg/commands/app.go @@ -151,14 +151,16 @@ func loadPluginCommands() []*cobra.Command { return commands } -func initConfig(configFile string) error { +func initConfig(configFile string, pathChanged bool) error { // Read from config viper.SetConfigFile(configFile) viper.SetConfigType("yaml") if err := viper.ReadInConfig(); err != nil { if errors.Is(err, os.ErrNotExist) { - log.Debug("Config file not found", log.FilePath(configFile)) - return nil + if !pathChanged { + log.Debug("Config file not found", log.FilePath(configFile)) + return nil + } } return xerrors.Errorf("config file %q loading error: %s", configFile, err) } @@ -201,7 +203,7 @@ func NewRootCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command { // Configure environment variables and config file // It cannot be called in init() because it must be called after viper.BindPFlags. - if err := initConfig(configPath); err != nil { + if err := initConfig(configPath, cmd.Flags().Changed(flag.ConfigFileFlag.ConfigName)); err != nil { return err } diff --git a/pkg/commands/app_test.go b/pkg/commands/app_test.go index 7235a3e94c7d..d6a3141190e9 100644 --- a/pkg/commands/app_test.go +++ b/pkg/commands/app_test.go @@ -296,6 +296,15 @@ func TestFlags(t *testing.T) { }, wantErr: `invalid argument "foo" for "--format" flag`, }, + { + name: "missing config file", + arguments: []string{ + "test", + "--config", + "none", + }, + wantErr: `config file "none" loading error: open none: no such file or directory`, + }, } for _, tt := range tests { From cfebe6ae1936a42f6b73e6b6542f31396a2407bc Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Tue, 16 Jul 2024 09:51:41 +0200 Subject: [PATCH 2/4] refactor: log a warning when default configuration is not found This will inform the user better when the default configuration file is not found. --- pkg/commands/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/app.go b/pkg/commands/app.go index 41c4ed78d509..629d63cdc649 100644 --- a/pkg/commands/app.go +++ b/pkg/commands/app.go @@ -158,7 +158,7 @@ func initConfig(configFile string, pathChanged bool) error { if err := viper.ReadInConfig(); err != nil { if errors.Is(err, os.ErrNotExist) { if !pathChanged { - log.Debug("Config file not found", log.FilePath(configFile)) + log.Warnf("Default config file %q not found, using built in values", log.FilePath(configFile)) return nil } } From 3d397f0072ee6f62cbc074f15f20c39a3d9a8a9d Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Wed, 17 Jul 2024 09:42:21 +0200 Subject: [PATCH 3/4] refactor: put log back to debug when default configuration is not found --- pkg/commands/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/app.go b/pkg/commands/app.go index 629d63cdc649..5a9baab6ca0b 100644 --- a/pkg/commands/app.go +++ b/pkg/commands/app.go @@ -158,7 +158,7 @@ func initConfig(configFile string, pathChanged bool) error { if err := viper.ReadInConfig(); err != nil { if errors.Is(err, os.ErrNotExist) { if !pathChanged { - log.Warnf("Default config file %q not found, using built in values", log.FilePath(configFile)) + log.Debugf("Default config file %q not found, using built in values", log.FilePath(configFile)) return nil } } From 300d2d8d621a73af9cc175cb47789925d3727656 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Thu, 18 Jul 2024 13:42:33 +0200 Subject: [PATCH 4/4] fix(app_test): simplify string to check for missing configuration error Part of the message is OS dependent so remove it to ensure it passes on all platforms. --- pkg/commands/app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/app_test.go b/pkg/commands/app_test.go index d6a3141190e9..308732ce918b 100644 --- a/pkg/commands/app_test.go +++ b/pkg/commands/app_test.go @@ -303,7 +303,7 @@ func TestFlags(t *testing.T) { "--config", "none", }, - wantErr: `config file "none" loading error: open none: no such file or directory`, + wantErr: `config file "none" loading error: open none:`, }, }