Skip to content

Commit

Permalink
chore: improve error message if config file not present
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Jones <bradley.jones@anchore.com>
  • Loading branch information
bradleyjones committed Mar 9, 2023
1 parent 04f11e8 commit 12d5dd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 15 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ 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)
switch err {
case nil:
break
case config.ConfigNotFoundErr:
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)
default:
fmt.Printf("Failed to load application config: \n\t%+v\n", err)
os.Exit(1)
}

appConfig = cfg
}

Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ var DefaultConfigValues = AppConfig{
PollingIntervalSeconds: 300,
}

var ConfigNotFoundErr error = 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)
Expand Down Expand Up @@ -174,7 +176,7 @@ func readConfig(v *viper.Viper, configPath, applicationName string) error {
return nil
}

return fmt.Errorf("application config not found")
return ConfigNotFoundErr
}

func (cfg AppConfig) String() string {
Expand Down

0 comments on commit 12d5dd3

Please sign in to comment.