config: Replace ~ with user's home directory path
Some checks failed
Test / lint (push) Has been cancelled
Test / test (1.17.x) (push) Has been cancelled
Test / test (1.18.x) (push) Has been cancelled
Test / test (1.19.x) (push) Has been cancelled
Test / test (1.20.x) (push) Has been cancelled
Test / test (1.21.x) (push) Has been cancelled
Test / test (1.22.x) (push) Has been cancelled
Test / test (1.23.x) (push) Has been cancelled
Test / test (1.24.x) (push) Has been cancelled

This commit is contained in:
T. R. Bernstein
2025-08-05 00:17:02 +02:00
parent 52ba6ab4b3
commit d2ae7b645f
3 changed files with 39 additions and 2 deletions

View File

@@ -352,6 +352,10 @@ func (c *Config) getMatchingHosts(alias string) []*Host {
return matchingHosts
}
func substituteTildeForHomeDirectory(value string) string {
return strings.Replace(value, "~", homedir(), 1)
}
func getKeyFromNodeForHost(node Node, alias, key string) (string, bool) {
lowerKey := strings.ToLower(key)
switch t := node.(type) {
@@ -363,12 +367,12 @@ func getKeyFromNodeForHost(node Node, alias, key string) (string, bool) {
panic("can't handle Match directives")
}
if lkey == lowerKey {
return t.Value, true
return substituteTildeForHomeDirectory(t.Value), true
}
case *Include:
val := t.Get(alias, key)
if val != "" {
return val, true
return substituteTildeForHomeDirectory(val), true
}
}
return "", false

View File

@@ -349,6 +349,34 @@ func TestIncludeString(t *testing.T) {
}
}
func TestResolvesTilde(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/configwithtilde"),
}
hosts := []string {
"specifc",
"unknown",
}
for _, host := range hosts {
t.Run(host, func(t *testing.T) {
val, err := us.GetStrict(host, "IdentityAgent")
if err != nil {
t.Errorf("expected nil error, got %v", err)
}
if !strings.Contains(val, homedir()) {
t.Errorf("expected value to have replaced ~ with user's home directory path, got %q", val)
}
if strings.Contains(val, "~/") {
t.Errorf("expected ~/ to have been replaced with user's home directory path, got %q", val)
}
})
}
}
var shellIncludeFile = []byte(`
# This host should not exist, so we can use it for test purposes / it won't
# interfere with any other configurations.

5
testdata/configwithtilde vendored Normal file
View File

@@ -0,0 +1,5 @@
Host specific
IdentityAgent "~/specific.sock"
Host *
IdentityAgent "~/wildcard.sock"