From 5232c981fac642671870ca3508834918f3f72f38 Mon Sep 17 00:00:00 2001 From: Bradley Jones Date: Thu, 9 Mar 2023 13:18:00 +0000 Subject: [PATCH] chore: improve error message if config file not present Signed-off-by: Bradley Jones --- cmd/cmd.go | 15 +++++++++++++-- internal/config/config.go | 4 +++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 5cc261c..0fb2b48 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "fmt" "os" @@ -42,10 +43,20 @@ func Execute() { func InitAppConfig() { cfg, err := config.LoadConfigFromFile(viper.GetViper(), &cliOnlyOpts) - if err != nil { - fmt.Printf("failed to load application config: \n\t%+v\n", err) + if errors.Is(err, config.ErrConfigNotFound) { + fmt.Println( + "Please ensure that a config file is specified with the --config flag or " + + "is present at one of the following locations:\n" + + "\t- ./anchore-ecs-inventory.yaml\n" + + "\t- ./.anchore-ecs-inventory/config.yaml\n" + + "\t- $HOME/anchore-ecs-inventory.yaml\n" + + "\t- $XDG_CONFIG_HOME/anchore-ecs-inventory/config.yaml") + os.Exit(1) + } else if err != nil { + fmt.Printf("Failed to load application config: \n\t%+v\n", err) os.Exit(1) } + appConfig = cfg } diff --git a/internal/config/config.go b/internal/config/config.go index 63101ae..7ec9614 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -62,6 +62,8 @@ var DefaultConfigValues = AppConfig{ PollingIntervalSeconds: 300, } +var ErrConfigNotFound = fmt.Errorf("application config not found") + func setDefaultValues(v *viper.Viper) { v.SetDefault("log.level", DefaultConfigValues.Log.Level) v.SetDefault("log.file", DefaultConfigValues.Log.FileLocation) @@ -174,7 +176,7 @@ func readConfig(v *viper.Viper, configPath, applicationName string) error { return nil } - return fmt.Errorf("application config not found") + return ErrConfigNotFound } func (cfg AppConfig) String() string {