12 alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
13 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*$`)
16 // ListOpts holds a list of values and a validation function.
17 type ListOpts struct {
19 validator ValidatorFctType
22 // NewListOpts creates a new ListOpts with the specified validator.
23 func NewListOpts(validator ValidatorFctType) ListOpts {
25 return *NewListOptsRef(&values, validator)
28 // NewListOptsRef creates a new ListOpts with the specified values and validator.
29 func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
36 func (opts *ListOpts) String() string {
37 return fmt.Sprintf("%v", []string((*opts.values)))
40 // Set validates if needed the input value and add it to the
42 func (opts *ListOpts) Set(value string) error {
43 if opts.validator != nil {
44 v, err := opts.validator(value)
50 (*opts.values) = append((*opts.values), value)
54 // Delete removes the specified element from the slice.
55 func (opts *ListOpts) Delete(key string) {
56 for i, k := range *opts.values {
58 (*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
64 // GetMap returns the content of values in a map in order to avoid
66 func (opts *ListOpts) GetMap() map[string]struct{} {
67 ret := make(map[string]struct{})
68 for _, k := range *opts.values {
74 // GetAll returns the values of slice.
75 func (opts *ListOpts) GetAll() []string {
79 // GetAllOrEmpty returns the values of the slice
80 // or an empty slice when there are no values.
81 func (opts *ListOpts) GetAllOrEmpty() []string {
84 return make([]string, 0)
89 // Get checks the existence of the specified key.
90 func (opts *ListOpts) Get(key string) bool {
91 for _, k := range *opts.values {
99 // Len returns the amount of element in the slice.
100 func (opts *ListOpts) Len() int {
101 return len((*opts.values))
104 //MapOpts holds a map of values and a validation function.
105 type MapOpts struct {
106 values map[string]string
107 validator ValidatorFctType
110 // Set validates if needed the input value and add it to the
111 // internal map, by splitting on '='.
112 func (opts *MapOpts) Set(value string) error {
113 if opts.validator != nil {
114 v, err := opts.validator(value)
120 vals := strings.SplitN(value, "=", 2)
122 (opts.values)[vals[0]] = ""
124 (opts.values)[vals[0]] = vals[1]
129 // GetAll returns the values of MapOpts as a map.
130 func (opts *MapOpts) GetAll() map[string]string {
134 func (opts *MapOpts) String() string {
135 return fmt.Sprintf("%v", map[string]string((opts.values)))
138 // NewMapOpts creates a new MapOpts with the specified map of values and a validator.
139 func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
141 values = make(map[string]string)
145 validator: validator,
149 // ValidatorFctType defines a validator function that returns a validated string and/or an error.
150 type ValidatorFctType func(val string) (string, error)
152 // ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
153 type ValidatorFctListType func(val string) ([]string, error)
155 // ValidateAttach validates that the specified string is a valid attach option.
156 func ValidateAttach(val string) (string, error) {
157 s := strings.ToLower(val)
158 for _, str := range []string{"stdin", "stdout", "stderr"} {
163 return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR")
166 // ValidateEnv validates an environment variable and returns it.
167 // If no value is specified, it returns the current value using os.Getenv.
169 // As on ParseEnvFile and related to #16585, environment variable names
170 // are not validate what so ever, it's up to application inside docker
171 // to validate them or not.
172 func ValidateEnv(val string) (string, error) {
173 arr := strings.Split(val, "=")
177 if !doesEnvExist(val) {
180 return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
183 // ValidateIPAddress validates an Ip address.
184 func ValidateIPAddress(val string) (string, error) {
185 var ip = net.ParseIP(strings.TrimSpace(val))
187 return ip.String(), nil
189 return "", fmt.Errorf("%s is not an ip address", val)
192 // ValidateMACAddress validates a MAC address.
193 func ValidateMACAddress(val string) (string, error) {
194 _, err := net.ParseMAC(strings.TrimSpace(val))
201 // ValidateDNSSearch validates domain for resolvconf search configuration.
202 // A zero length domain is represented by a dot (.).
203 func ValidateDNSSearch(val string) (string, error) {
204 if val = strings.Trim(val, " "); val == "." {
207 return validateDomain(val)
210 func validateDomain(val string) (string, error) {
211 if alphaRegexp.FindString(val) == "" {
212 return "", fmt.Errorf("%s is not a valid domain", val)
214 ns := domainRegexp.FindSubmatch([]byte(val))
215 if len(ns) > 0 && len(ns[1]) < 255 {
216 return string(ns[1]), nil
218 return "", fmt.Errorf("%s is not a valid domain", val)
221 // ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
222 // ExtraHost are in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6).
223 func ValidateExtraHost(val string) (string, error) {
224 // allow for IPv6 addresses in extra hosts by only splitting on first ":"
225 arr := strings.SplitN(val, ":", 2)
226 if len(arr) != 2 || len(arr[0]) == 0 {
227 return "", fmt.Errorf("bad format for add-host: %q", val)
229 if _, err := ValidateIPAddress(arr[1]); err != nil {
230 return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
235 // ValidateLabel validates that the specified string is a valid label, and returns it.
236 // Labels are in the form on key=value.
237 func ValidateLabel(val string) (string, error) {
238 if strings.Count(val, "=") < 1 {
239 return "", fmt.Errorf("bad attribute format: %s", val)
244 func doesEnvExist(name string) bool {
245 for _, entry := range os.Environ() {
246 parts := strings.SplitN(entry, "=", 2)
247 if parts[0] == name {