Skip to content

Commit

Permalink
Add pwd to config search path iff it contains a config
Browse files Browse the repository at this point in the history
Only add the pwd to the config search path if and only if it contains
a config file that we expect.  This avoids incorrectly finding config
files that may be specific to applictions other than syft.

fixes: anchore#1634

Signed-off-by: Aidan Delaney <adelaney21@bloomberg.net>
  • Loading branch information
AidanDelaney committed Mar 2, 2023
1 parent 98e737f commit 51398ab
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"os"
"path"
"reflect"
"sort"
Expand Down Expand Up @@ -223,13 +224,16 @@ func loadConfig(v *viper.Viper, configPath string) error {

// start searching for valid configs in order...
// 1. look for .<appname>.yaml (in the current directory)
v.AddConfigPath(".")
v.SetConfigName("." + internal.ApplicationName)
if err = v.ReadInConfig(); err == nil {
v.Set("config", v.ConfigFileUsed())
return nil
} else if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
return fmt.Errorf("unable to parse config=%q: %w", v.ConfigFileUsed(), err)
confFilePath := "." + internal.ApplicationName
if _, err := os.Stat(confFilePath); err == nil {
v.AddConfigPath(".")
v.SetConfigName(confFilePath)
if err = v.ReadInConfig(); err == nil {
v.Set("config", v.ConfigFileUsed())
return nil
} else if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
return fmt.Errorf("unable to parse config=%q: %w", v.ConfigFileUsed(), err)
}
}

// 2. look for .<appname>/config.yaml (in the current directory)
Expand Down

0 comments on commit 51398ab

Please sign in to comment.