Add log opts flag to pass in logging options

Signed-off-by: wlan0 <sidharthamn@gmail.com>
This commit is contained in:
wlan0 2015-05-04 14:39:48 -07:00 committed by Vincent Demeester
parent 7da142413e
commit d0c32b1efa
2 changed files with 86 additions and 0 deletions

View File

@ -28,6 +28,14 @@ func ListVar(values *[]string, names []string, usage string) {
flag.Var(newListOptsRef(values, nil), names, usage) flag.Var(newListOptsRef(values, nil), names, usage)
} }
func MapVar(values map[string]string, names []string, usage string) {
flag.Var(newMapOpt(values, nil), names, usage)
}
func LogOptsVar(values map[string]string, names []string, usage string) {
flag.Var(newMapOpt(values, ValidateLogOpts), names, usage)
}
func HostListVar(values *[]string, names []string, usage string) { func HostListVar(values *[]string, names []string, usage string) {
flag.Var(newListOptsRef(values, ValidateHost), names, usage) flag.Var(newListOptsRef(values, ValidateHost), names, usage)
} }
@ -130,10 +138,53 @@ func (opts *ListOpts) Len() int {
return len((*opts.values)) return len((*opts.values))
} }
//MapOpts type
type MapOpts struct {
values map[string]string
validator ValidatorFctType
}
func (opts *MapOpts) Set(value string) error {
if opts.validator != nil {
v, err := opts.validator(value)
if err != nil {
return err
}
value = v
}
vals := strings.SplitN(value, "=", 2)
if len(vals) == 1 {
(opts.values)[vals[0]] = ""
} else {
(opts.values)[vals[0]] = vals[1]
}
return nil
}
func (opts *MapOpts) String() string {
return fmt.Sprintf("%v", map[string]string((opts.values)))
}
func newMapOpt(values map[string]string, validator ValidatorFctType) *MapOpts {
return &MapOpts{
values: values,
validator: validator,
}
}
// Validators // Validators
type ValidatorFctType func(val string) (string, error) type ValidatorFctType func(val string) (string, error)
type ValidatorFctListType func(val string) ([]string, error) type ValidatorFctListType func(val string) ([]string, error)
func ValidateLogOpts(val string) (string, error) {
allowedKeys := map[string]string{}
vals := strings.Split(val, "=")
if allowedKeys[vals[0]] != "" {
return val, nil
}
return "", fmt.Errorf("%s is not a valid log opt", vals[0])
}
func ValidateAttach(val string) (string, error) { func ValidateAttach(val string) (string, error) {
s := strings.ToLower(val) s := strings.ToLower(val)
for _, str := range []string{"stdin", "stdout", "stderr"} { for _, str := range []string{"stdin", "stdout", "stderr"} {

View File

@ -1,6 +1,7 @@
package opts package opts
import ( import (
"fmt"
"strings" "strings"
"testing" "testing"
) )
@ -28,6 +29,31 @@ func TestValidateIPAddress(t *testing.T) {
} }
func TestMapOpts(t *testing.T) {
tmpMap := make(map[string]string)
o := newMapOpt(tmpMap, logOptsValidator)
o.Set("max-size=1")
if o.String() != "map[max-size:1]" {
t.Errorf("%s != [map[max-size:1]", o.String())
}
o.Set("max-file=2")
if len(tmpMap) != 2 {
t.Errorf("map length %d != 2", len(tmpMap))
}
if tmpMap["max-file"] != "2" {
t.Errorf("max-file = %s != 2", tmpMap["max-file"])
}
if tmpMap["max-size"] != "1" {
t.Errorf("max-size = %s != 1", tmpMap["max-size"])
}
if o.Set("dummy-val=3") == nil {
t.Errorf("validator is not being called")
}
}
func TestValidateMACAddress(t *testing.T) { func TestValidateMACAddress(t *testing.T) {
if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil { if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil {
t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err) t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err)
@ -152,3 +178,12 @@ func TestValidateExtraHosts(t *testing.T) {
} }
} }
} }
func logOptsValidator(val string) (string, error) {
allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
vals := strings.Split(val, "=")
if allowedKeys[vals[0]] != "" {
return val, nil
}
return "", fmt.Errorf("invalid key %s", vals[0])
}