提交 58868374 编写于 作者: M Mislav Marohnić

Expand tokens found in ssh config `HostName`

Per "TOKENS" section in ssh_config(5)

Fixes #1936
上级 b3b3ebb8
......@@ -61,7 +61,7 @@ func (r *SSHConfigReader) readFile(c SSHConfig, re *regexp.Regexp, f string) err
} else {
for _, host := range hosts {
for _, name := range names {
c[host] = name
c[host] = expandTokens(name, host)
}
}
}
......@@ -69,3 +69,8 @@ func (r *SSHConfigReader) readFile(c SSHConfig, re *regexp.Regexp, f string) err
return scanner.Err()
}
func expandTokens(text, host string) string {
expanded := strings.Replace(text, "%h", host, -1)
return strings.Replace(expanded, "%%", "%", -1)
}
......@@ -24,3 +24,17 @@ func TestSSHConfigReader_Read(t *testing.T) {
sc := r.Read()
assert.Equal(t, "ssh.github.com", sc["github.com"])
}
func TestSSHConfigReader_ExpandTokens(t *testing.T) {
f, _ := ioutil.TempFile("", "ssh-config")
c := `Host github.com example.org
Hostname 1-%h-2-%%-3-%h-%%
`
ioutil.WriteFile(f.Name(), []byte(c), os.ModePerm)
r := &SSHConfigReader{[]string{f.Name()}}
sc := r.Read()
assert.Equal(t, "1-github.com-2-%-3-github.com-%", sc["github.com"])
assert.Equal(t, "1-example.org-2-%-3-example.org-%", sc["example.org"])
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册