Skip to content

Commit

Permalink
Add the executable dir to the config load path
Browse files Browse the repository at this point in the history
In adwatchd we would like to use the executable directory as the default
config path.

Update the config API to add the executable path to the config search
path, after all the other paths.
  • Loading branch information
GabrielNagy committed Jun 16, 2022
1 parent 7383096 commit 04923cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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 {
Expand Down
39 changes: 30 additions & 9 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -170,6 +177,19 @@ func TestInit(t *testing.T) {
tc := tc
t.Run(name, func(t *testing.T) {
configDir := t.TempDir()
prefix := "adsys_config_test"

if tc.withConfigInExeDir {
exePath, err := os.Executable()
require.NoError(t, err, "Setup: can't get executable path")
configDir = filepath.Dir(exePath)

t.Cleanup(func() {
os.Remove(filepath.Join(configDir, prefix+".yaml"))
syscall.Sync()
})
}

if !tc.notInConfigDir {
chDir(t, configDir)
}
Expand Down Expand Up @@ -204,7 +224,6 @@ func TestInit(t *testing.T) {
require.NoError(t, err, "Setup: can’t set config flag")
}

prefix := "adsys_config_test"
if tc.withConfigEnv {
testutils.Setenv(t, strings.ToUpper(prefix)+"_VALUE", "envvalue")
}
Expand All @@ -215,6 +234,7 @@ func TestInit(t *testing.T) {
}
err = os.WriteFile(filepath.Join(configDir, prefix+".yaml"), []byte(tc.configFileContent), 0600)
require.NoError(t, err, "Setup: failed to write initial config file")

}

result := struct {
Expand Down Expand Up @@ -272,6 +292,7 @@ func TestInit(t *testing.T) {

err = os.WriteFile(filepath.Join(configDir, prefix+".yaml"), []byte(tc.changeConfigWith), 0600)
require.NoError(t, err, "Setup: failed to write initial config file")

// Let’s force a sync to make sure the file is written on disk
syscall.Sync()
select {
Expand Down

0 comments on commit 04923cf

Please sign in to comment.