diff --git a/README.md b/README.md index 6b0e1b2..37667ba 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ See [test-data/input.json](test-data/input.json) for full example. In the **host }, "region-b": { "selenium-cloud-b-[1:40].example.com": { - "port": 4445, + "ports": "444[5:8]", "count": 2 } } diff --git a/cmd/data.go b/cmd/data.go index a723497..a783135 100644 --- a/cmd/data.go +++ b/cmd/data.go @@ -2,6 +2,7 @@ package cmd type Host struct { Port int `json:"port"` + Ports string `json:"ports"` Count int `json:"count"` Username string `json:"username"` Password string `json:"password"` diff --git a/cmd/generate.go b/cmd/generate.go index 488013e..5fff837 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -126,17 +126,20 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region { for hostPattern, host := range region { hostNames := parseHostPattern(hostPattern) for _, hostName := range hostNames { - h := ggr.Host{ - Name: hostName, - Port: host.Port, - Count: host.Count, - Username: host.Username, - Password: host.Password, + ports := getPorts(host.Port, host.Ports) + for _, port := range ports { + h := ggr.Host{ + Name: hostName, + Port: port, + Count: host.Count, + Username: host.Username, + Password: host.Password, + } + if host.VNC != "" { + h.VNC = preProcessVNC(hostName, port, host.VNC) + } + xmlHosts = append(xmlHosts, h) } - if host.VNC != "" { - h.VNC = preProcessVNC(hostName, host.Port, host.VNC) - } - xmlHosts = append(xmlHosts, h) } } xmlRegions = append(xmlRegions, ggr.Region{ @@ -147,6 +150,19 @@ func jsonRegionsToXmlRegions(regions Regions) []ggr.Region { return xmlRegions } +func getPorts(port int, ports string) []int { + if ports != "" { + var ret []int + for _, maybePort := range parseHostPattern(ports) { + if port, err := strconv.Atoi(maybePort); err == nil { + ret = append(ret, port) + } + } + return ret + } + return []int{port} +} + func preProcessVNC(hostName string, port int, vnc string) string { const selenoid = "selenoid" const hostPattern = "$hostName" diff --git a/cmd/generate_test.go b/cmd/generate_test.go index b75ab7e..64a6307 100644 --- a/cmd/generate_test.go +++ b/cmd/generate_test.go @@ -112,3 +112,11 @@ func TestPreProcessVNC(t *testing.T) { AssertThat(t, preProcessVNC("selenoid-host.example.com", 4444, "selenoid"), EqualTo{"ws://selenoid-host.example.com:4444/vnc"}) AssertThat(t, preProcessVNC("vnc-host.example.com", 5900, "vnc://$hostName:5900"), EqualTo{"vnc://vnc-host.example.com:5900"}) } + +func TestGetPorts(t *testing.T) { + AssertThat(t, getPorts(4444, ""), EqualTo{[]int{4444}}) + AssertThat(t, getPorts(4444, "4445"), EqualTo{[]int{4445}}) + AssertThat(t, getPorts(4444, "444[5:8]"), EqualTo{[]int{4445, 4446, 4447, 4448}}) + AssertThat(t, getPorts(4444, "44[5:8]4"), EqualTo{[]int{4454, 4464, 4474, 4484}}) + AssertThat(t, len(getPorts(4444, "NaN")), EqualTo{0}) +}