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

Allow specifying a literal `%h` in `HostName` ssh config

上级 58868374
......@@ -71,6 +71,14 @@ func (r *SSHConfigReader) readFile(c SSHConfig, re *regexp.Regexp, f string) err
}
func expandTokens(text, host string) string {
expanded := strings.Replace(text, "%h", host, -1)
return strings.Replace(expanded, "%%", "%", -1)
re := regexp.MustCompile(`%[%h]`)
return re.ReplaceAllStringFunc(text, func(match string) string {
switch match {
case "%h":
return host
case "%%":
return "%"
}
return ""
})
}
......@@ -28,13 +28,13 @@ func TestSSHConfigReader_Read(t *testing.T) {
func TestSSHConfigReader_ExpandTokens(t *testing.T) {
f, _ := ioutil.TempFile("", "ssh-config")
c := `Host github.com example.org
Hostname 1-%h-2-%%-3-%h-%%
Hostname 1-%h-2-%%h-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"])
assert.Equal(t, "1-github.com-2-%h-3-github.com-%", sc["github.com"])
assert.Equal(t, "1-example.org-2-%h-3-example.org-%", sc["example.org"])
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册