Merge pull request #1205 from dalehamel/parse-cidrs

Add support for passing CIDRs to config
pull/1242/head
dlorenc 2017-03-14 10:49:07 -07:00 committed by GitHub
commit 7f8f29d28e
2 changed files with 10 additions and 0 deletions

View File

@ -76,6 +76,12 @@ func setElement(e reflect.Value, v string) error {
return fmt.Errorf("Error converting input %s to an IP.", v) return fmt.Errorf("Error converting input %s to an IP.", v)
} }
e.Set(reflect.ValueOf(ip)) e.Set(reflect.ValueOf(ip))
case net.IPNet:
_, cidr, err := net.ParseCIDR(v)
if err != nil {
return fmt.Errorf("Error converting input %s to a CIDR: %s", v, err)
}
e.Set(reflect.ValueOf(*cidr))
case utilnet.PortRange: case utilnet.PortRange:
pr, err := utilnet.ParsePortRange(v) pr, err := utilnet.ParsePortRange(v)
if err != nil { if err != nil {

View File

@ -57,9 +57,11 @@ type subConfig3 struct {
R utilnet.PortRange R utilnet.PortRange
S []string S []string
T aliasedString T aliasedString
U net.IPNet
} }
func buildConfig() testConfig { func buildConfig() testConfig {
_, cidr, _ := net.ParseCIDR("12.34.56.78/16")
return testConfig{ return testConfig{
A: "foo", A: "foo",
B: 1, B: 1,
@ -75,6 +77,7 @@ func buildConfig() testConfig {
P: false, P: false,
Q: net.ParseIP("12.34.56.78"), Q: net.ParseIP("12.34.56.78"),
R: utilnet.PortRange{Base: 2, Size: 4}, R: utilnet.PortRange{Base: 2, Size: 4},
U: *cidr,
}, },
}, },
E: &subConfig2{ E: &subConfig2{
@ -176,6 +179,7 @@ func TestSetElement(t *testing.T) {
{"D.I.R", "7-11", func(t testConfig) bool { return t.D.I.R.Base == 7 && t.D.I.R.Size == 5 }}, {"D.I.R", "7-11", func(t testConfig) bool { return t.D.I.R.Base == 7 && t.D.I.R.Size == 5 }},
{"D.I.S", "a,b", func(t testConfig) bool { return reflect.DeepEqual(t.D.I.S, []string{"a", "b"}) }}, {"D.I.S", "a,b", func(t testConfig) bool { return reflect.DeepEqual(t.D.I.S, []string{"a", "b"}) }},
{"D.I.T", "foo", func(t testConfig) bool { return t.D.I.T == "foo" }}, {"D.I.T", "foo", func(t testConfig) bool { return t.D.I.T == "foo" }},
{"D.I.U", "11.22.0.0/16", func(t testConfig) bool { return t.D.I.U.String() == "11.22.0.0/16" }},
} { } {
a := buildConfig() a := buildConfig()
if err := FindAndSet(tc.path, &a, tc.newval); err != nil { if err := FindAndSet(tc.path, &a, tc.newval); err != nil {