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