123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package opts
- import (
- "fmt"
- "net"
- "regexp"
- "strings"
- )
- var (
- 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*$`)
- )
- // ListOpts holds a list of values and a validation function.
- type ListOpts struct {
- values *[]string
- validator ValidatorFctType
- }
- // NewListOpts creates a new ListOpts with the specified validator.
- func NewListOpts(validator ValidatorFctType) ListOpts {
- var values []string
- return *NewListOptsRef(&values, validator)
- }
- // NewListOptsRef creates a new ListOpts with the specified values and validator.
- func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
- return &ListOpts{
- values: values,
- validator: validator,
- }
- }
- func (opts *ListOpts) String() string {
- return fmt.Sprintf("%v", []string((*opts.values)))
- }
- // 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
- }
- (*opts.values) = append((*opts.values), value)
- return nil
- }
- // Delete removes the specified element from the slice.
- func (opts *ListOpts) Delete(key string) {
- for i, k := range *opts.values {
- if k == key {
- (*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
- 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{})
- for _, k := range *opts.values {
- ret[k] = struct{}{}
- }
- return ret
- }
- // GetAll returns the values of slice.
- func (opts *ListOpts) GetAll() []string {
- return (*opts.values)
- }
- // 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
- }
- // Get checks the existence of the specified key.
- func (opts *ListOpts) Get(key string) bool {
- for _, k := range *opts.values {
- if k == key {
- return true
- }
- }
- return false
- }
- // Len returns the amount of element in the slice.
- func (opts *ListOpts) Len() int {
- return len((*opts.values))
- }
- //MapOpts holds a map of values and a validation function.
- type MapOpts struct {
- values map[string]string
- validator ValidatorFctType
- }
- // Set validates if needed the input value and add it to the
- // internal map, by splitting on '='.
- 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
- }
- // GetAll returns the values of MapOpts as a map.
- func (opts *MapOpts) GetAll() map[string]string {
- return opts.values
- }
- func (opts *MapOpts) String() string {
- return fmt.Sprintf("%v", map[string]string((opts.values)))
- }
- // NewMapOpts creates a new MapOpts with the specified map of values and a validator.
- func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
- if values == nil {
- values = make(map[string]string)
- }
- return &MapOpts{
- values: values,
- validator: validator,
- }
- }
- // ValidatorFctType defines a validator function that returns a validated string and/or an error.
- type ValidatorFctType func(val string) (string, error)
- // ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
- type ValidatorFctListType func(val string) ([]string, error)
- // ValidateIPAddress validates an Ip address.
- func ValidateIPAddress(val string) (string, error) {
- var ip = net.ParseIP(strings.TrimSpace(val))
- if ip != nil {
- return ip.String(), nil
- }
- return "", fmt.Errorf("%s is not an ip address", val)
- }
- // ValidateDNSSearch validates domain for resolvconf search configuration.
- // A zero length domain is represented by a dot (.).
- func ValidateDNSSearch(val string) (string, error) {
- if val = strings.Trim(val, " "); val == "." {
- return val, nil
- }
- return validateDomain(val)
- }
- func validateDomain(val string) (string, error) {
- if alphaRegexp.FindString(val) == "" {
- return "", fmt.Errorf("%s is not a valid domain", val)
- }
- ns := domainRegexp.FindSubmatch([]byte(val))
- if len(ns) > 0 && len(ns[1]) < 255 {
- return string(ns[1]), nil
- }
- return "", fmt.Errorf("%s is not a valid domain", val)
- }
- // ValidateLabel validates that the specified string is a valid label, and returns it.
- // Labels are in the form on key=value.
- func ValidateLabel(val string) (string, error) {
- if strings.Count(val, "=") < 1 {
- return "", fmt.Errorf("bad attribute format: %s", val)
- }
- return val, nil
- }
|