Opts lint issues, ip and ulimit

Signed-off-by: Sevki Hasirci <s@sevki.org>
This commit is contained in:
Sevki Hasirci 2015-07-21 19:15:36 +03:00 committed by Vincent Demeester
parent f2934dbacf
commit 409920de5c
3 changed files with 18 additions and 8 deletions

View File

@ -5,20 +5,23 @@ import (
"net" "net"
) )
// IpOpt type that hold an IP // IPOpt type that hold an IP
type IpOpt struct { type IPOpt struct {
*net.IP *net.IP
} }
func NewIpOpt(ref *net.IP, defaultVal string) *IpOpt { // NewIPOpt returns a new IPOpt from a string of an IP.
o := &IpOpt{ func NewIPOpt(ref *net.IP, defaultVal string) *IPOpt {
o := &IPOpt{
IP: ref, IP: ref,
} }
o.Set(defaultVal) o.Set(defaultVal)
return o return o
} }
func (o *IpOpt) Set(val string) error { // Set sets an IPv4 or IPv6 address from a given string. If the given
// string is not parsable as an IP address it will return an error.
func (o *IPOpt) Set(val string) error {
ip := net.ParseIP(val) ip := net.ParseIP(val)
if ip == nil { if ip == nil {
return fmt.Errorf("%s is not an ip address", val) return fmt.Errorf("%s is not an ip address", val)
@ -27,7 +30,9 @@ func (o *IpOpt) Set(val string) error {
return nil return nil
} }
func (o *IpOpt) String() string { // String returns the IP address stored in the IPOpt. If IPOpt is a
// nil pointer, it returns an empty string.
func (o *IPOpt) String() string {
if *o.IP == nil { if *o.IP == nil {
return "" return ""
} }

View File

@ -45,9 +45,9 @@ func TestIpOptSetInvalidVal(t *testing.T) {
ip := net.IPv4(127, 0, 0, 1) ip := net.IPv4(127, 0, 0, 1)
ipOpt := &IpOpt{IP: &ip} ipOpt := &IpOpt{IP: &ip}
invalidIp := "invalid ip" invalidIP := "invalid ip"
expectedError := "invalid ip is not an ip address" expectedError := "invalid ip is not an ip address"
err := ipOpt.Set(invalidIp) err := ipOpt.Set(invalidIP)
if err == nil || err.Error() != expectedError { if err == nil || err.Error() != expectedError {
t.Fatalf("Expected an Error with [%v], got [%v]", expectedError, err.Error()) t.Fatalf("Expected an Error with [%v], got [%v]", expectedError, err.Error())
} }

View File

@ -6,10 +6,12 @@ import (
"github.com/docker/docker/pkg/ulimit" "github.com/docker/docker/pkg/ulimit"
) )
// UlimitOpt defines a map of Ulimits
type UlimitOpt struct { type UlimitOpt struct {
values *map[string]*ulimit.Ulimit values *map[string]*ulimit.Ulimit
} }
// NewUlimitOpt creates a new UlimitOpt
func NewUlimitOpt(ref *map[string]*ulimit.Ulimit) *UlimitOpt { func NewUlimitOpt(ref *map[string]*ulimit.Ulimit) *UlimitOpt {
if ref == nil { if ref == nil {
ref = &map[string]*ulimit.Ulimit{} ref = &map[string]*ulimit.Ulimit{}
@ -17,6 +19,7 @@ func NewUlimitOpt(ref *map[string]*ulimit.Ulimit) *UlimitOpt {
return &UlimitOpt{ref} return &UlimitOpt{ref}
} }
// Set validates a Ulimit and sets its name as a key in UlimitOpt
func (o *UlimitOpt) Set(val string) error { func (o *UlimitOpt) Set(val string) error {
l, err := ulimit.Parse(val) l, err := ulimit.Parse(val)
if err != nil { if err != nil {
@ -28,6 +31,7 @@ func (o *UlimitOpt) Set(val string) error {
return nil return nil
} }
// String returns Ulimit values as a string.
func (o *UlimitOpt) String() string { func (o *UlimitOpt) String() string {
var out []string var out []string
for _, v := range *o.values { for _, v := range *o.values {
@ -37,6 +41,7 @@ func (o *UlimitOpt) String() string {
return fmt.Sprintf("%v", out) return fmt.Sprintf("%v", out)
} }
// GetList returns a slice of pointers to Ulimits.
func (o *UlimitOpt) GetList() []*ulimit.Ulimit { func (o *UlimitOpt) GetList() []*ulimit.Ulimit {
var ulimits []*ulimit.Ulimit var ulimits []*ulimit.Ulimit
for _, v := range *o.values { for _, v := range *o.values {