github.com/sbward/docker@v1.4.2-0.20150114010528-c9dab702bed3/opts/opts.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os"
     7  	"path"
     8  	"regexp"
     9  	"strings"
    10  
    11  	"github.com/docker/docker/api"
    12  	flag "github.com/docker/docker/pkg/mflag"
    13  	"github.com/docker/docker/pkg/parsers"
    14  )
    15  
    16  var (
    17  	alphaRegexp  = regexp.MustCompile(`[a-zA-Z]`)
    18  	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*$`)
    19  )
    20  
    21  func ListVar(values *[]string, names []string, usage string) {
    22  	flag.Var(newListOptsRef(values, nil), names, usage)
    23  }
    24  
    25  func HostListVar(values *[]string, names []string, usage string) {
    26  	flag.Var(newListOptsRef(values, api.ValidateHost), names, usage)
    27  }
    28  
    29  func IPListVar(values *[]string, names []string, usage string) {
    30  	flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage)
    31  }
    32  
    33  func DnsSearchListVar(values *[]string, names []string, usage string) {
    34  	flag.Var(newListOptsRef(values, ValidateDnsSearch), names, usage)
    35  }
    36  
    37  func IPVar(value *net.IP, names []string, defaultValue, usage string) {
    38  	flag.Var(NewIpOpt(value, defaultValue), names, usage)
    39  }
    40  
    41  func LabelListVar(values *[]string, names []string, usage string) {
    42  	flag.Var(newListOptsRef(values, ValidateLabel), names, usage)
    43  }
    44  
    45  // ListOpts type
    46  type ListOpts struct {
    47  	values    *[]string
    48  	validator ValidatorFctType
    49  }
    50  
    51  func NewListOpts(validator ValidatorFctType) ListOpts {
    52  	var values []string
    53  	return *newListOptsRef(&values, validator)
    54  }
    55  
    56  func newListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
    57  	return &ListOpts{
    58  		values:    values,
    59  		validator: validator,
    60  	}
    61  }
    62  
    63  func (opts *ListOpts) String() string {
    64  	return fmt.Sprintf("%v", []string((*opts.values)))
    65  }
    66  
    67  // Set validates if needed the input value and add it to the
    68  // internal slice.
    69  func (opts *ListOpts) Set(value string) error {
    70  	if opts.validator != nil {
    71  		v, err := opts.validator(value)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		value = v
    76  	}
    77  	(*opts.values) = append((*opts.values), value)
    78  	return nil
    79  }
    80  
    81  // Delete remove the given element from the slice.
    82  func (opts *ListOpts) Delete(key string) {
    83  	for i, k := range *opts.values {
    84  		if k == key {
    85  			(*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
    86  			return
    87  		}
    88  	}
    89  }
    90  
    91  // GetMap returns the content of values in a map in order to avoid
    92  // duplicates.
    93  // FIXME: can we remove this?
    94  func (opts *ListOpts) GetMap() map[string]struct{} {
    95  	ret := make(map[string]struct{})
    96  	for _, k := range *opts.values {
    97  		ret[k] = struct{}{}
    98  	}
    99  	return ret
   100  }
   101  
   102  // GetAll returns the values' slice.
   103  // FIXME: Can we remove this?
   104  func (opts *ListOpts) GetAll() []string {
   105  	return (*opts.values)
   106  }
   107  
   108  // Get checks the existence of the given key.
   109  func (opts *ListOpts) Get(key string) bool {
   110  	for _, k := range *opts.values {
   111  		if k == key {
   112  			return true
   113  		}
   114  	}
   115  	return false
   116  }
   117  
   118  // Len returns the amount of element in the slice.
   119  func (opts *ListOpts) Len() int {
   120  	return len((*opts.values))
   121  }
   122  
   123  // Validators
   124  type ValidatorFctType func(val string) (string, error)
   125  type ValidatorFctListType func(val string) ([]string, error)
   126  
   127  func ValidateAttach(val string) (string, error) {
   128  	s := strings.ToLower(val)
   129  	for _, str := range []string{"stdin", "stdout", "stderr"} {
   130  		if s == str {
   131  			return s, nil
   132  		}
   133  	}
   134  	return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR.")
   135  }
   136  
   137  func ValidateLink(val string) (string, error) {
   138  	if _, err := parsers.PartParser("name:alias", val); err != nil {
   139  		return val, err
   140  	}
   141  	return val, nil
   142  }
   143  
   144  func ValidatePath(val string) (string, error) {
   145  	var containerPath string
   146  
   147  	if strings.Count(val, ":") > 2 {
   148  		return val, fmt.Errorf("bad format for volumes: %s", val)
   149  	}
   150  
   151  	splited := strings.SplitN(val, ":", 2)
   152  	if len(splited) == 1 {
   153  		containerPath = splited[0]
   154  		val = path.Clean(splited[0])
   155  	} else {
   156  		containerPath = splited[1]
   157  		val = fmt.Sprintf("%s:%s", splited[0], path.Clean(splited[1]))
   158  	}
   159  
   160  	if !path.IsAbs(containerPath) {
   161  		return val, fmt.Errorf("%s is not an absolute path", containerPath)
   162  	}
   163  	return val, nil
   164  }
   165  
   166  func ValidateEnv(val string) (string, error) {
   167  	arr := strings.Split(val, "=")
   168  	if len(arr) > 1 {
   169  		return val, nil
   170  	}
   171  	return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
   172  }
   173  
   174  func ValidateIPAddress(val string) (string, error) {
   175  	var ip = net.ParseIP(strings.TrimSpace(val))
   176  	if ip != nil {
   177  		return ip.String(), nil
   178  	}
   179  	return "", fmt.Errorf("%s is not an ip address", val)
   180  }
   181  
   182  // Validates domain for resolvconf search configuration.
   183  // A zero length domain is represented by .
   184  func ValidateDnsSearch(val string) (string, error) {
   185  	if val = strings.Trim(val, " "); val == "." {
   186  		return val, nil
   187  	}
   188  	return validateDomain(val)
   189  }
   190  
   191  func validateDomain(val string) (string, error) {
   192  	if alphaRegexp.FindString(val) == "" {
   193  		return "", fmt.Errorf("%s is not a valid domain", val)
   194  	}
   195  	ns := domainRegexp.FindSubmatch([]byte(val))
   196  	if len(ns) > 0 {
   197  		return string(ns[1]), nil
   198  	}
   199  	return "", fmt.Errorf("%s is not a valid domain", val)
   200  }
   201  
   202  func ValidateExtraHost(val string) (string, error) {
   203  	arr := strings.Split(val, ":")
   204  	if len(arr) != 2 || len(arr[0]) == 0 {
   205  		return "", fmt.Errorf("bad format for add-host: %s", val)
   206  	}
   207  	if _, err := ValidateIPAddress(arr[1]); err != nil {
   208  		return "", fmt.Errorf("bad format for add-host: %s", val)
   209  	}
   210  	return val, nil
   211  }
   212  
   213  func ValidateLabel(val string) (string, error) {
   214  	if strings.Count(val, "=") != 1 {
   215  		return "", fmt.Errorf("bad attribute format: %s", val)
   216  	}
   217  	return val, nil
   218  }