Skip to content

Commit

Permalink
Merge pull request #32 from anchore/config-error
Browse files Browse the repository at this point in the history
chore: improve error message if config file not present
  • Loading branch information
bradleyjones committed Mar 9, 2023
2 parents 04f11e8 + 5232c98 commit 7121838
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 13 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -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
}

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 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)
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 ErrConfigNotFound
}

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

0 comments on commit 7121838

Please sign in to comment.