Skip to content

Commit

Permalink
Fix: expanding variables in HostName
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 11, 2015
1 parent efc5236 commit fbe06bd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ hosts:
Inherits:
- schooltemplate

"somehost[0-7]*":
# ssh somehost2042 -> ssh somehost2042.some.zone
HostName: "%h.some.zone"

vm-*.school.com:
# ssh vm-42.school.com -> ssh vm-42.school.com/gw.school.com -l student -o ForwardX11=yes -i ~/.ssh/school-rsa
Expand Down Expand Up @@ -207,6 +210,7 @@ Get a released version on: https://github.com/moul/advanced-ssh-config/releases
### master (unreleased)

* Fix: inheritance was not working for non assh-related fields ([#54](https://github.com/moul/advanced-ssh-config/issues/54))
* Fix: expanding variables in HostName ([#56](https://github.com/moul/advanced-ssh-config/issues/56))

[Full commits list](https://github.com/moul/advanced-ssh-config/compare/v2.0.0...master)

Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ func computeHost(host *Host, config *Config, name string, fullCompute bool) (*Ho
if fullCompute {
// apply defaults based on "Host *"
computedHost.ApplyDefaults(&config.Defaults)

if computedHost.HostName == "" {
computedHost.HostName = name
}
// expands variables in host
// i.e: %h.some.zone -> {name}.some.zone
hostname := strings.Replace(computedHost.HostName, "%h", "%n", -1)
computedHost.HostName = computedHost.ExpandString(hostname)
}

return computedHost, nil
Expand Down
43 changes: 40 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ hosts:
jjj:
HostName: "%h.jjjjj"
"*.kkk":
HostName: "%h.kkkkk"
defaults:
Port: 22
User: root
Expand Down Expand Up @@ -161,7 +164,7 @@ func TestConfig_LoadConfig(t *testing.T) {
config := New()
err := config.LoadConfig(strings.NewReader(yamlConfig))
So(err, ShouldBeNil)
So(len(config.Hosts), ShouldEqual, 10)
So(len(config.Hosts), ShouldEqual, 11)
So(config.Hosts["aaa"].HostName, ShouldEqual, "1.2.3.4")
So(config.Hosts["aaa"].Port, ShouldEqual, uint(0))
So(config.Hosts["aaa"].User, ShouldEqual, "")
Expand Down Expand Up @@ -248,6 +251,9 @@ func TestConfig_JsonSring(t *testing.T) {
"*.ddd": {
"HostName": "1.3.5.7"
},
"*.kkk": {
"HostName": "%h.kkkkk"
},
"aaa": {
"HostName": "1.2.3.4"
},
Expand Down Expand Up @@ -310,6 +316,37 @@ func TestConfig_JsonSring(t *testing.T) {
})
}

func TestComputeHost(t *testing.T) {
Convey("Testing computeHost()", t, func() {
config := New()
err := config.LoadConfig(strings.NewReader(yamlConfig))
So(err, ShouldBeNil)

Convey("Standard", func() {

})
Convey("Expand variables in HostName", func() {
host := config.Hosts["jjj"]
computed, err := computeHost(&host, config, "jjj", false)
So(err, ShouldBeNil)
So(computed.HostName, ShouldEqual, "%h.jjjjj")

computed, err = computeHost(&host, config, "jjj", true)
So(err, ShouldBeNil)
So(computed.HostName, ShouldEqual, "jjj.jjjjj")

host = config.Hosts["*.kkk"]
computed, err = computeHost(&host, config, "test.kkk", false)
So(err, ShouldBeNil)
So(computed.HostName, ShouldEqual, "%h.kkkkk")

computed, err = computeHost(&host, config, "test.kkk", true)
So(err, ShouldBeNil)
So(computed.HostName, ShouldEqual, "test.kkk.kkkkk")
})
})
}

func TestConfig_getHostByName(t *testing.T) {
Convey("Testing Config.getHostByName", t, func() {
config := dummyConfig()
Expand Down Expand Up @@ -425,7 +462,7 @@ func TestConfig_LoadFiles(t *testing.T) {
So(err, ShouldBeNil)
So(config.includedFiles[file.Name()], ShouldEqual, true)
So(len(config.includedFiles), ShouldEqual, 1)
So(len(config.Hosts), ShouldEqual, 10)
So(len(config.Hosts), ShouldEqual, 11)
So(config.Hosts["aaa"].HostName, ShouldEqual, "1.2.3.4")
So(config.Hosts["aaa"].Port, ShouldEqual, uint(0))
So(config.Hosts["aaa"].User, ShouldEqual, "")
Expand All @@ -448,7 +485,7 @@ func TestConfig_LoadFiles(t *testing.T) {
So(err, ShouldBeNil)
So(config.includedFiles[file.Name()], ShouldEqual, true)
So(len(config.includedFiles), ShouldEqual, 1)
So(len(config.Hosts), ShouldEqual, 10)
So(len(config.Hosts), ShouldEqual, 11)
So(config.Hosts["aaa"].HostName, ShouldEqual, "1.2.3.4")
So(config.Hosts["aaa"].Port, ShouldEqual, uint(0))
So(config.Hosts["aaa"].User, ShouldEqual, "")
Expand Down
21 changes: 21 additions & 0 deletions pkg/config/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Host struct {

// private assh fields
name string `yaml:- json:-`
inputName string `yaml:- json:-`
isDefault bool `yaml:- json:-`
inherited map[string]bool `yaml:- json:-`
}
Expand Down Expand Up @@ -396,6 +397,9 @@ func (h *Host) ApplyDefaults(defaults *Host) {

// private assh fields
// h.inherited = make(map[string]bool, 0)
if h.inputName == "" {
h.inputName = h.name
}

// Extra defaults
if h.Port == 0 {
Expand Down Expand Up @@ -686,8 +690,25 @@ func (h *Host) WriteSshConfigTo(w io.Writer) error {

func (h *Host) ExpandString(input string) string {
output := input

// name of the host in config
output = strings.Replace(output, "%name", h.Name(), -1)

// original target host name specified on the command line
output = strings.Replace(output, "%n", h.inputName, -1)

// target host name
output = strings.Replace(output, "%h", h.HostName, -1)

// port
output = strings.Replace(output, "%p", fmt.Sprintf("%d", h.Port), -1)

// FIXME: add
// %L -> first component of the local host name
// %l -> local host name
// %r -> remote login username
// %u -> username of the user running assh
// %r -> remote login username

return output
}

0 comments on commit fbe06bd

Please sign in to comment.