sic v2 client WP4: parseTarget — split host/hop1/hop2 into host + ordered hops
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
func parseTarget(s string) (host string, hops []string) {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == '/' {
|
||||
host = s[:i]
|
||||
hops = splitHops(s[i+1:])
|
||||
return
|
||||
}
|
||||
}
|
||||
host = s
|
||||
hops = nil
|
||||
return
|
||||
}
|
||||
|
||||
func splitHops(s string) []string {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
var hops []string
|
||||
start := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == '/' {
|
||||
hops = append(hops, s[start:i])
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
hops = append(hops, s[start:])
|
||||
return hops
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// WP4: parseTarget splits "host/hop1/hop2" into the ssh host + the ordered nested hops. A bare
|
||||
// host (no '/') has no hops. Pure split; validation (empty host, hop count vs nest depth) is
|
||||
// resolveVerbs' job, not this one.
|
||||
func TestParseTarget(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
host string
|
||||
hops []string
|
||||
}{
|
||||
{"boltzmann", "boltzmann", nil},
|
||||
{"boltzmann/memory", "boltzmann", []string{"memory"}},
|
||||
{"boltzmann/memory/stash-stash-1", "boltzmann", []string{"memory", "stash-stash-1"}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
host, hops := parseTarget(tc.in)
|
||||
if host != tc.host || !reflect.DeepEqual(hops, tc.hops) {
|
||||
t.Fatalf("parseTarget(%q) = (%q, %#v), want (%q, %#v)", tc.in, host, hops, tc.host, tc.hops)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user