adding ip as an option for flags

Signed-off-by: Isabel Jimenez <contact.isabeljimenez@gmail.com>
This commit is contained in:
Isabel Jimenez 2015-05-18 19:29:48 -04:00
parent 19d51f4a1a
commit 8001455ecf
2 changed files with 28 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package cluster package cluster
import ( import (
"net"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -49,3 +50,11 @@ func (do DriverOpts) Float(key, env string) (float64, bool) {
} }
return 0.0, false return 0.0, false
} }
// IP returns an IP address from the driver options
func (do DriverOpts) IP(key, env string) (net.IP, bool) {
if value, ok := do.String(key, env); ok {
return net.ParseIP(value), true
}
return nil, false
}

View File

@ -1,13 +1,14 @@
package cluster package cluster
import ( import (
"net"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var opts = DriverOpts{"foo1=bar", "foo2=-5", "foo3=7", "foo4=0.6"} var opts = DriverOpts{"foo1=bar", "foo2=-5", "foo3=7", "foo4=0.6", "foo5=127.0.0.1"}
func TestString(t *testing.T) { func TestString(t *testing.T) {
os.Setenv("FOO_4", "bar") os.Setenv("FOO_4", "bar")
@ -125,3 +126,20 @@ func TestFloat(t *testing.T) {
assert.False(t, ok) assert.False(t, ok)
assert.Equal(t, val, 0.0) assert.Equal(t, val, 0.0)
} }
func TestIP(t *testing.T) {
os.Setenv("FOO_4", "0.0.0.0")
defer os.Setenv("FOO_4", "")
val, ok := opts.IP("foo5", "")
assert.True(t, ok)
assert.Equal(t, val, net.ParseIP("127.0.0.1"))
val, ok = opts.IP("", "FOO_4")
assert.True(t, ok)
assert.Equal(t, val, net.ParseIP("0.0.0.0"))
val, ok = opts.IP("invalid", "")
assert.False(t, ok)
assert.Equal(t, val, net.IP(nil))
}