Convert for util.IP to just use a net.IP
pflag can handle IP addresses so use the pflag code instead of doing it ourselves. This means our code just uses net.IP and we don't have all of the useless casting back and forth!
This commit is contained in:
@@ -17,30 +17,10 @@ limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IP adapts net.IP for use as a flag.
|
||||
type IP net.IP
|
||||
|
||||
func (ip IP) String() string {
|
||||
return net.IP(ip).String()
|
||||
}
|
||||
|
||||
func (ip *IP) Set(value string) error {
|
||||
*ip = IP(net.ParseIP(strings.TrimSpace(value)))
|
||||
if *ip == nil {
|
||||
return fmt.Errorf("invalid IP address: '%s'", value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*IP) Type() string {
|
||||
return "ip"
|
||||
}
|
||||
|
||||
// IPNet adapts net.IPNet for use as a flag.
|
||||
type IPNet net.IPNet
|
||||
|
||||
|
@@ -22,45 +22,6 @@ import (
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func TestIP(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
success bool
|
||||
expected string
|
||||
}{
|
||||
{"0.0.0.0", true, "0.0.0.0"},
|
||||
{" 0.0.0.0 ", true, "0.0.0.0"},
|
||||
{"1.2.3.4", true, "1.2.3.4"},
|
||||
{"127.0.0.1", true, "127.0.0.1"},
|
||||
{"255.255.255.255", true, "255.255.255.255"},
|
||||
{"", false, ""},
|
||||
{"0", false, ""},
|
||||
{"localhost", false, ""},
|
||||
{"0.0.0", false, ""},
|
||||
{"0.0.0.", false, ""},
|
||||
{"0.0.0.0.", false, ""},
|
||||
{"0.0.0.256", false, ""},
|
||||
{"0 . 0 . 0 . 0", false, ""},
|
||||
}
|
||||
|
||||
for i := range testCases {
|
||||
tc := &testCases[i]
|
||||
var f flag.Value = &IP{}
|
||||
err := f.Set(tc.input)
|
||||
if err != nil && tc.success == true {
|
||||
t.Errorf("expected success, got %q", err)
|
||||
continue
|
||||
} else if err == nil && tc.success == false {
|
||||
t.Errorf("expected failure")
|
||||
continue
|
||||
} else if tc.success {
|
||||
if f.String() != tc.expected {
|
||||
t.Errorf("expected %q, got %q", tc.expected, f.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPNet(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
|
Reference in New Issue
Block a user