Use HOME for Barnard config path
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/pelletier/go-toml/v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -398,15 +397,14 @@ func fileExists(path string) bool {
|
|||||||
|
|
||||||
func resolvePath(path string) string {
|
func resolvePath(path string) string {
|
||||||
if strings.HasPrefix(path, "~/") || strings.Contains(path, "$HOME") {
|
if strings.HasPrefix(path, "~/") || strings.Contains(path, "$HOME") {
|
||||||
usr, err := user.Current()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
var hd = usr.HomeDir
|
|
||||||
if strings.Contains(path, "$HOME") {
|
if strings.Contains(path, "$HOME") {
|
||||||
path = strings.Replace(path, "$HOME", hd, 1)
|
path = strings.Replace(path, "$HOME", homeDir, 1)
|
||||||
} else {
|
} else {
|
||||||
path = strings.Replace(path, "~", hd, 1)
|
path = strings.Replace(path, "~", homeDir, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return path
|
return path
|
||||||
|
|||||||
@@ -36,3 +36,16 @@ func TestConfigBackfillsRecordingDefaults(t *testing.T) {
|
|||||||
t.Fatalf("expected admin menu f11, got %s", got)
|
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