Use HOME for Barnard config path
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -398,15 +397,14 @@ func fileExists(path string) bool {
|
||||
|
||||
func resolvePath(path string) string {
|
||||
if strings.HasPrefix(path, "~/") || strings.Contains(path, "$HOME") {
|
||||
usr, err := user.Current()
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var hd = usr.HomeDir
|
||||
if strings.Contains(path, "$HOME") {
|
||||
path = strings.Replace(path, "$HOME", hd, 1)
|
||||
path = strings.Replace(path, "$HOME", homeDir, 1)
|
||||
} else {
|
||||
path = strings.Replace(path, "~", hd, 1)
|
||||
path = strings.Replace(path, "~", homeDir, 1)
|
||||
}
|
||||
}
|
||||
return path
|
||||
|
||||
@@ -36,3 +36,16 @@ func TestConfigBackfillsRecordingDefaults(t *testing.T) {
|
||||
t.Fatalf("expected admin menu f11, got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigUsesHomeEnvironmentForDefaultPath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("HOME", dir)
|
||||
configPath := "~/.barnard.toml"
|
||||
|
||||
cfg := NewConfig(&configPath)
|
||||
cfg.SaveConfig()
|
||||
|
||||
if _, err := os.Stat(filepath.Join(dir, ".barnard.toml")); err != nil {
|
||||
t.Fatalf("expected config to be written under HOME: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user