opts/opts.go
e6e320ac
 package opts
1ba11384
 
 import (
 	"fmt"
899e9e74
 	"net"
3cd9b2aa
 	"regexp"
1ba11384
 	"strings"
 )
 
4119c9d7
 var (
dfc6c04f
 	alphaRegexp  = regexp.MustCompile(`[a-zA-Z]`)
 	domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
4119c9d7
 )
 
0bd016b1
 // ListOpts holds a list of values and a validation function.
1ba11384
 type ListOpts struct {
62000026
 	values    *[]string
1ba11384
 	validator ValidatorFctType
 }
 
0bd016b1
 // NewListOpts creates a new ListOpts with the specified validator.
1ba11384
 func NewListOpts(validator ValidatorFctType) ListOpts {
62000026
 	var values []string
96ce3a19
 	return *NewListOptsRef(&values, validator)
62000026
 }
 
0bd016b1
 // NewListOptsRef creates a new ListOpts with the specified values and validator.
96ce3a19
 func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
62000026
 	return &ListOpts{
 		values:    values,
1ba11384
 		validator: validator,
 	}
 }
 
 func (opts *ListOpts) String() string {
62000026
 	return fmt.Sprintf("%v", []string((*opts.values)))
1ba11384
 }
 
 // Set validates if needed the input value and add it to the
 // internal slice.
 func (opts *ListOpts) Set(value string) error {
 	if opts.validator != nil {
 		v, err := opts.validator(value)
 		if err != nil {
 			return err
 		}
 		value = v
 	}
62000026
 	(*opts.values) = append((*opts.values), value)
1ba11384
 	return nil
 }
 
0bd016b1
 // Delete removes the specified element from the slice.
1ba11384
 func (opts *ListOpts) Delete(key string) {
62000026
 	for i, k := range *opts.values {
1ba11384
 		if k == key {
62000026
 			(*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
1ba11384
 			return
 		}
 	}
 }
 
 // GetMap returns the content of values in a map in order to avoid
 // duplicates.
 func (opts *ListOpts) GetMap() map[string]struct{} {
 	ret := make(map[string]struct{})
62000026
 	for _, k := range *opts.values {
1ba11384
 		ret[k] = struct{}{}
 	}
 	return ret
 }
 
0bd016b1
 // GetAll returns the values of slice.
1ba11384
 func (opts *ListOpts) GetAll() []string {
62000026
 	return (*opts.values)
1ba11384
 }
 
f1a74a89
 // GetAllOrEmpty returns the values of the slice
 // or an empty slice when there are no values.
 func (opts *ListOpts) GetAllOrEmpty() []string {
 	v := *opts.values
 	if v == nil {
 		return make([]string, 0)
 	}
 	return v
 }
 
0bd016b1
 // Get checks the existence of the specified key.
1ba11384
 func (opts *ListOpts) Get(key string) bool {
62000026
 	for _, k := range *opts.values {
1ba11384
 		if k == key {
 			return true
 		}
 	}
 	return false
 }
 
 // Len returns the amount of element in the slice.
 func (opts *ListOpts) Len() int {
62000026
 	return len((*opts.values))
1ba11384
 }
 
677a6b35
 // NamedOption is an interface that list and map options
 // with names implement.
 type NamedOption interface {
 	Name() string
 }
 
 // NamedListOpts is a ListOpts with a configuration name.
 // This struct is useful to keep reference to the assigned
 // field name in the internal configuration struct.
 type NamedListOpts struct {
 	name string
 	ListOpts
 }
 
 var _ NamedOption = &NamedListOpts{}
 
 // NewNamedListOptsRef creates a reference to a new NamedListOpts struct.
 func NewNamedListOptsRef(name string, values *[]string, validator ValidatorFctType) *NamedListOpts {
 	return &NamedListOpts{
 		name:     name,
 		ListOpts: *NewListOptsRef(values, validator),
 	}
 }
 
 // Name returns the name of the NamedListOpts in the configuration.
 func (o *NamedListOpts) Name() string {
 	return o.name
 }
 
0bd016b1
 //MapOpts holds a map of values and a validation function.
dca9e02b
 type MapOpts struct {
 	values    map[string]string
 	validator ValidatorFctType
 }
 
dfc6c04f
 // Set validates if needed the input value and add it to the
 // internal map, by splitting on '='.
dca9e02b
 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
 }
 
0bd016b1
 // GetAll returns the values of MapOpts as a map.
b3b7eb27
 func (opts *MapOpts) GetAll() map[string]string {
 	return opts.values
 }
 
dca9e02b
 func (opts *MapOpts) String() string {
 	return fmt.Sprintf("%v", map[string]string((opts.values)))
 }
 
0bd016b1
 // NewMapOpts creates a new MapOpts with the specified map of values and a validator.
96ce3a19
 func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
 	if values == nil {
 		values = make(map[string]string)
 	}
dca9e02b
 	return &MapOpts{
 		values:    values,
 		validator: validator,
 	}
 }
 
677a6b35
 // NamedMapOpts is a MapOpts struct with a configuration name.
 // This struct is useful to keep reference to the assigned
 // field name in the internal configuration struct.
 type NamedMapOpts struct {
 	name string
 	MapOpts
 }
 
 var _ NamedOption = &NamedMapOpts{}
 
 // NewNamedMapOpts creates a reference to a new NamedMapOpts struct.
 func NewNamedMapOpts(name string, values map[string]string, validator ValidatorFctType) *NamedMapOpts {
 	return &NamedMapOpts{
 		name:    name,
 		MapOpts: *NewMapOpts(values, validator),
 	}
 }
 
 // Name returns the name of the NamedMapOpts in the configuration.
 func (o *NamedMapOpts) Name() string {
 	return o.name
 }
 
0bd016b1
 // ValidatorFctType defines a validator function that returns a validated string and/or an error.
1ba11384
 type ValidatorFctType func(val string) (string, error)
dfc6c04f
 
0bd016b1
 // ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
568f86eb
 type ValidatorFctListType func(val string) ([]string, error)
1ba11384
 
0bd016b1
 // ValidateIPAddress validates an Ip address.
b6811171
 func ValidateIPAddress(val string) (string, error) {
899e9e74
 	var ip = net.ParseIP(strings.TrimSpace(val))
 	if ip != nil {
 		return ip.String(), nil
3cd9b2aa
 	}
899e9e74
 	return "", fmt.Errorf("%s is not an ip address", val)
3cd9b2aa
 }
fbfac21e
 
0bd016b1
 // ValidateDNSSearch validates domain for resolvconf search configuration.
 // A zero length domain is represented by a dot (.).
dfc6c04f
 func ValidateDNSSearch(val string) (string, error) {
804b00cd
 	if val = strings.Trim(val, " "); val == "." {
 		return val, nil
 	}
 	return validateDomain(val)
 }
 
 func validateDomain(val string) (string, error) {
4119c9d7
 	if alphaRegexp.FindString(val) == "" {
fbfac21e
 		return "", fmt.Errorf("%s is not a valid domain", val)
 	}
4119c9d7
 	ns := domainRegexp.FindSubmatch([]byte(val))
b2f05c22
 	if len(ns) > 0 && len(ns[1]) < 255 {
fbfac21e
 		return string(ns[1]), nil
 	}
 	return "", fmt.Errorf("%s is not a valid domain", val)
 }
69a75c67
 
0bd016b1
 // ValidateLabel validates that the specified string is a valid label, and returns it.
 // Labels are in the form on key=value.
2fe36baa
 func ValidateLabel(val string) (string, error) {
dfc6c04f
 	if strings.Count(val, "=") < 1 {
2fe36baa
 		return "", fmt.Errorf("bad attribute format: %s", val)
 	}
 	return val, nil
 }