diff --git a/internal/config/config.go b/internal/config/config.go index 27cf44672..bb4b9b9a9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,6 +4,8 @@ import ( "context" "errors" "fmt" + "os" + "path/filepath" "github.com/fsnotify/fsnotify" "github.com/sirupsen/logrus" @@ -54,6 +56,14 @@ func Init(name string, rootCmd cobra.Command, vip *viper.Viper, configChanged fu vip.AddConfigPath("./") vip.AddConfigPath("$HOME/") vip.AddConfigPath("/etc/") + + // Add the executable path to the config search path. + binPath, err := os.Executable() + if err != nil { + log.Warningf(context.Background(), i18n.G("Failed to get current executable path, not adding it as a config dir: %v"), err) + } else { + vip.AddConfigPath(filepath.Dir(binPath)) + } } if err := vip.ReadInConfig(); err != nil { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 155548030..2fe40ac88 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -86,14 +86,15 @@ func TestSetVerboseMode(t *testing.T) { func TestInit(t *testing.T) { tests := map[string]struct { - withValueFlagSet bool - noVerboseFlag bool - noConfigFlag bool - withConfigFlagSet string - withConfigEnv bool - configFileContent string - notInConfigDir bool - changeConfigWith string + withValueFlagSet bool + noVerboseFlag bool + noConfigFlag bool + withConfigFlagSet string + withConfigEnv bool + withConfigInExeDir bool + configFileContent string + notInConfigDir bool + changeConfigWith string errFromCallbackOn int @@ -108,6 +109,12 @@ func TestInit(t *testing.T) { configFileContent: "value: filecontentvalue", want: "filecontentvalue", wantCallbackCalled: 1, }, + "Load configuration from executable dir": { + configFileContent: "value: filecontentvalue", + withConfigInExeDir: true, + notInConfigDir: true, + want: "filecontentvalue", wantCallbackCalled: 1, + }, "No config flag set before Init is call is ignored": { noConfigFlag: true, wantCallbackCalled: 1, @@ -170,6 +177,13 @@ func TestInit(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { configDir := t.TempDir() + + if tc.withConfigInExeDir { + exePath, err := os.Executable() + require.NoError(t, err, "Setup: can't get executable path") + configDir = filepath.Dir(exePath) + } + if !tc.notInConfigDir { chDir(t, configDir) }