pkg/types/flags: introduce flags package

I want to use the Addrs type in another experimental proxy that I am
implementing. Pull it out into a separate package.
This commit is contained in:
Brandon Philips
2014-09-27 16:24:26 -07:00
parent 2a0f3d85c8
commit 78a9bba276
6 changed files with 127 additions and 107 deletions

51
pkg/flags/addrs.go Normal file
View File

@@ -0,0 +1,51 @@
package flags
import (
"errors"
"net"
"strconv"
"strings"
)
// Addrs implements the flag.Value interface to allow users to define multiple
// listen addresses on the command-line
type Addrs []string
// Set parses a command line set of listen addresses, formatted like:
// 127.0.0.1:7001,10.1.1.2:80
func (as *Addrs) Set(s string) error {
parsed := make([]string, 0)
for _, in := range strings.Split(s, ",") {
a := strings.TrimSpace(in)
if err := validateAddr(a); err != nil {
return err
}
parsed = append(parsed, a)
}
if len(parsed) == 0 {
return errors.New("no valid addresses given!")
}
*as = parsed
return nil
}
func (as *Addrs) String() string {
return strings.Join(*as, ",")
}
// validateAddr ensures that the provided string is a valid address. Valid
// addresses are of the form IP:port.
// Returns an error if the address is invalid, else nil.
func validateAddr(s string) error {
parts := strings.SplitN(s, ":", 2)
if len(parts) != 2 {
return errors.New("bad format in address specification")
}
if net.ParseIP(parts[0]) == nil {
return errors.New("bad IP in address specification")
}
if _, err := strconv.Atoi(parts[1]); err != nil {
return errors.New("bad port in address specification")
}
return nil
}

42
pkg/flags/addrs_test.go Normal file
View File

@@ -0,0 +1,42 @@
package flags
import (
"testing"
)
func TestBadValidateAddr(t *testing.T) {
tests := []string{
// bad IP specification
":4001",
"127.0:8080",
"123:456",
// bad port specification
"127.0.0.1:foo",
"127.0.0.1:",
// unix sockets not supported
"unix://",
"unix://tmp/etcd.sock",
// bad strings
"somewhere",
"234#$",
"file://foo/bar",
"http://hello",
}
for i, in := range tests {
if err := validateAddr(in); err == nil {
t.Errorf(`#%d: unexpected nil error for in=%q`, i, in)
}
}
}
func TestValidateAddr(t *testing.T) {
tests := []string{
"1.2.3.4:8080",
"10.1.1.1:80",
}
for i, in := range tests {
if err := validateAddr(in); err != nil {
t.Errorf("#%d: err=%v, want nil for in=%q", i, err, in)
}
}
}

39
pkg/flags/proxy.go Normal file
View File

@@ -0,0 +1,39 @@
package flags
import (
"errors"
)
const (
ProxyValueOff = "off"
ProxyValueReadonly = "readonly"
ProxyValueOn = "on"
)
var (
ProxyValues = []string{
ProxyValueOff,
ProxyValueReadonly,
ProxyValueOn,
}
)
// ProxyFlag implements the flag.Value interface.
type Proxy string
// Set verifies the argument to be a valid member of proxyFlagValues
// before setting the underlying flag value.
func (pf *Proxy) Set(s string) error {
for _, v := range ProxyValues {
if s == v {
*pf = Proxy(s)
return nil
}
}
return errors.New("invalid value")
}
func (pf *Proxy) String() string {
return string(*pf)
}

28
pkg/flags/proxy_test.go Normal file
View File

@@ -0,0 +1,28 @@
package flags
import (
"testing"
)
func TestProxySet(t *testing.T) {
tests := []struct {
val string
pass bool
}{
// known values
{"on", true},
{"off", true},
// unrecognized values
{"foo", false},
{"", false},
}
for i, tt := range tests {
pf := new(Proxy)
err := pf.Set(tt.val)
if tt.pass != (err == nil) {
t.Errorf("#%d: want pass=%t, but got err=%v", i, tt.pass, err)
}
}
}