opts/opts.go
e6e320ac
 package opts
1ba11384
 
 import (
 	"fmt"
899e9e74
 	"net"
1ba11384
 	"os"
5a38680b
 	"path"
3cd9b2aa
 	"regexp"
1ba11384
 	"strings"
b6811171
 
62000026
 	"github.com/docker/docker/api"
 	flag "github.com/docker/docker/pkg/mflag"
43981084
 	"github.com/docker/docker/pkg/parsers"
3f390506
 	"github.com/docker/docker/pkg/ulimit"
9ab73260
 	"github.com/docker/docker/utils"
1ba11384
 )
 
4119c9d7
 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*$`)
 )
 
62000026
 func ListVar(values *[]string, names []string, usage string) {
 	flag.Var(newListOptsRef(values, nil), names, usage)
 }
 
 func HostListVar(values *[]string, names []string, usage string) {
 	flag.Var(newListOptsRef(values, api.ValidateHost), names, usage)
 }
 
 func IPListVar(values *[]string, names []string, usage string) {
 	flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage)
 }
 
 func DnsSearchListVar(values *[]string, names []string, usage string) {
 	flag.Var(newListOptsRef(values, ValidateDnsSearch), names, usage)
 }
 
 func IPVar(value *net.IP, names []string, defaultValue, usage string) {
 	flag.Var(NewIpOpt(value, defaultValue), names, usage)
 }
 
2fe36baa
 func LabelListVar(values *[]string, names []string, usage string) {
 	flag.Var(newListOptsRef(values, ValidateLabel), names, usage)
 }
 
3f390506
 func UlimitMapVar(values map[string]*ulimit.Ulimit, names []string, usage string) {
 	flag.Var(NewUlimitOpt(values), names, usage)
 }
 
1ba11384
 // ListOpts type
 type ListOpts struct {
62000026
 	values    *[]string
1ba11384
 	validator ValidatorFctType
 }
 
 func NewListOpts(validator ValidatorFctType) ListOpts {
62000026
 	var values []string
 	return *newListOptsRef(&values, validator)
 }
 
 func newListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
 	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
 }
 
 // Delete remove the given element from the slice.
 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.
 // FIXME: can we remove this?
 func (opts *ListOpts) GetMap() map[string]struct{} {
 	ret := make(map[string]struct{})
62000026
 	for _, k := range *opts.values {
1ba11384
 		ret[k] = struct{}{}
 	}
 	return ret
 }
 
 // GetAll returns the values' slice.
 // FIXME: Can we remove this?
 func (opts *ListOpts) GetAll() []string {
62000026
 	return (*opts.values)
1ba11384
 }
 
 // Get checks the existence of the given key.
 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
 }
 
 // Validators
 type ValidatorFctType func(val string) (string, error)
568f86eb
 type ValidatorFctListType func(val string) ([]string, error)
1ba11384
 
 func ValidateAttach(val string) (string, error) {
a7134132
 	s := strings.ToLower(val)
 	for _, str := range []string{"stdin", "stdout", "stderr"} {
 		if s == str {
 			return s, nil
 		}
1ba11384
 	}
a7134132
 	return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR.")
1ba11384
 }
 
 func ValidateLink(val string) (string, error) {
43981084
 	if _, err := parsers.PartParser("name:alias", val); err != nil {
1ba11384
 		return val, err
 	}
 	return val, nil
 }
 
 func ValidatePath(val string) (string, error) {
 	var containerPath string
 
2bbc90e9
 	if strings.Count(val, ":") > 2 {
 		return val, fmt.Errorf("bad format for volumes: %s", val)
 	}
 
1ba11384
 	splited := strings.SplitN(val, ":", 2)
 	if len(splited) == 1 {
 		containerPath = splited[0]
5a38680b
 		val = path.Clean(splited[0])
1ba11384
 	} else {
 		containerPath = splited[1]
5a38680b
 		val = fmt.Sprintf("%s:%s", splited[0], path.Clean(splited[1]))
1ba11384
 	}
 
5a38680b
 	if !path.IsAbs(containerPath) {
1ba11384
 		return val, fmt.Errorf("%s is not an absolute path", containerPath)
 	}
 	return val, nil
 }
 
 func ValidateEnv(val string) (string, error) {
 	arr := strings.Split(val, "=")
 	if len(arr) > 1 {
 		return val, nil
 	}
9ab73260
 	if !utils.DoesEnvExist(val) {
 		return val, nil
 	}
1ba11384
 	return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
 }
 
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
 
2ba0fbb0
 func ValidateMACAddress(val string) (string, error) {
 	_, err := net.ParseMAC(strings.TrimSpace(val))
 	if err != nil {
 		return "", err
 	} else {
 		return val, nil
 	}
 }
 
804b00cd
 // Validates domain for resolvconf search configuration.
 // A zero length domain is represented by .
 func ValidateDnsSearch(val string) (string, error) {
 	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
 
68e48b65
 func ValidateExtraHost(val string) (string, error) {
fdfa2057
 	// allow for IPv6 addresses in extra hosts by only splitting on first ":"
 	arr := strings.SplitN(val, ":", 2)
68e48b65
 	if len(arr) != 2 || len(arr[0]) == 0 {
1ce9fa1c
 		return "", fmt.Errorf("bad format for add-host: %q", val)
68e48b65
 	}
 	if _, err := ValidateIPAddress(arr[1]); err != nil {
1ce9fa1c
 		return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
68e48b65
 	}
 	return val, nil
 }
 
2fe36baa
 func ValidateLabel(val string) (string, error) {
 	if strings.Count(val, "=") != 1 {
 		return "", fmt.Errorf("bad attribute format: %s", val)
 	}
 	return val, nil
 }