2014-02-11 21:46:55 -05:00
|
|
|
package opts
|
2013-11-26 12:46:06 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-13 08:02:12 -04:00
|
|
|
"net"
|
2014-07-18 14:48:19 -04:00
|
|
|
"net/url"
|
2013-11-26 12:46:06 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-11-22 12:55:27 -05:00
|
|
|
"regexp"
|
2013-11-26 12:46:06 -05:00
|
|
|
"strings"
|
2014-07-09 17:47:55 -04:00
|
|
|
|
2014-08-09 21:13:44 -04:00
|
|
|
"github.com/docker/docker/api"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2014-07-28 20:23:38 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2013-11-26 12:46:06 -05:00
|
|
|
)
|
|
|
|
|
2014-09-15 23:30:10 -04:00
|
|
|
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*$`)
|
|
|
|
)
|
|
|
|
|
2014-08-09 21:13:44 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-07-18 14:48:19 -04:00
|
|
|
func MirrorListVar(values *[]string, names []string, usage string) {
|
|
|
|
flag.Var(newListOptsRef(values, ValidateMirror), names, usage)
|
|
|
|
}
|
|
|
|
|
2013-11-26 12:46:06 -05:00
|
|
|
// ListOpts type
|
|
|
|
type ListOpts struct {
|
2014-08-09 21:13:44 -04:00
|
|
|
values *[]string
|
2013-11-26 12:46:06 -05:00
|
|
|
validator ValidatorFctType
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewListOpts(validator ValidatorFctType) ListOpts {
|
2014-08-09 21:13:44 -04:00
|
|
|
var values []string
|
|
|
|
return *newListOptsRef(&values, validator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
|
|
|
|
return &ListOpts{
|
|
|
|
values: values,
|
2013-11-26 12:46:06 -05:00
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *ListOpts) String() string {
|
2014-08-09 21:13:44 -04:00
|
|
|
return fmt.Sprintf("%v", []string((*opts.values)))
|
2013-11-26 12:46:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2014-08-09 21:13:44 -04:00
|
|
|
(*opts.values) = append((*opts.values), value)
|
2013-11-26 12:46:06 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete remove the given element from the slice.
|
|
|
|
func (opts *ListOpts) Delete(key string) {
|
2014-08-09 21:13:44 -04:00
|
|
|
for i, k := range *opts.values {
|
2013-11-26 12:46:06 -05:00
|
|
|
if k == key {
|
2014-08-09 21:13:44 -04:00
|
|
|
(*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
|
2013-11-26 12:46:06 -05:00
|
|
|
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{})
|
2014-08-09 21:13:44 -04:00
|
|
|
for _, k := range *opts.values {
|
2013-11-26 12:46:06 -05:00
|
|
|
ret[k] = struct{}{}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll returns the values' slice.
|
|
|
|
// FIXME: Can we remove this?
|
|
|
|
func (opts *ListOpts) GetAll() []string {
|
2014-08-09 21:13:44 -04:00
|
|
|
return (*opts.values)
|
2013-11-26 12:46:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get checks the existence of the given key.
|
|
|
|
func (opts *ListOpts) Get(key string) bool {
|
2014-08-09 21:13:44 -04:00
|
|
|
for _, k := range *opts.values {
|
2013-11-26 12:46:06 -05:00
|
|
|
if k == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the amount of element in the slice.
|
|
|
|
func (opts *ListOpts) Len() int {
|
2014-08-09 21:13:44 -04:00
|
|
|
return len((*opts.values))
|
2013-11-26 12:46:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validators
|
|
|
|
type ValidatorFctType func(val string) (string, error)
|
|
|
|
|
|
|
|
func ValidateAttach(val string) (string, error) {
|
2014-07-30 10:20:52 -04:00
|
|
|
s := strings.ToLower(val)
|
|
|
|
for _, str := range []string{"stdin", "stdout", "stderr"} {
|
|
|
|
if s == str {
|
|
|
|
return s, nil
|
|
|
|
}
|
2013-11-26 12:46:06 -05:00
|
|
|
}
|
2014-07-30 10:20:52 -04:00
|
|
|
return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR.")
|
2013-11-26 12:46:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateLink(val string) (string, error) {
|
2014-07-28 20:23:38 -04:00
|
|
|
if _, err := parsers.PartParser("name:alias", val); err != nil {
|
2013-11-26 12:46:06 -05:00
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidatePath(val string) (string, error) {
|
|
|
|
var containerPath string
|
|
|
|
|
2013-11-28 15:24:04 -05:00
|
|
|
if strings.Count(val, ":") > 2 {
|
|
|
|
return val, fmt.Errorf("bad format for volumes: %s", val)
|
|
|
|
}
|
|
|
|
|
2013-11-26 12:46:06 -05:00
|
|
|
splited := strings.SplitN(val, ":", 2)
|
|
|
|
if len(splited) == 1 {
|
|
|
|
containerPath = splited[0]
|
|
|
|
val = filepath.Clean(splited[0])
|
|
|
|
} else {
|
|
|
|
containerPath = splited[1]
|
|
|
|
val = fmt.Sprintf("%s:%s", splited[0], filepath.Clean(splited[1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !filepath.IsAbs(containerPath) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
|
|
|
|
}
|
|
|
|
|
2014-07-09 17:47:55 -04:00
|
|
|
func ValidateIPAddress(val string) (string, error) {
|
2014-06-13 08:02:12 -04:00
|
|
|
var ip = net.ParseIP(strings.TrimSpace(val))
|
|
|
|
if ip != nil {
|
|
|
|
return ip.String(), nil
|
2013-11-22 12:55:27 -05:00
|
|
|
}
|
2014-06-13 08:02:12 -04:00
|
|
|
return "", fmt.Errorf("%s is not an ip address", val)
|
2013-11-22 12:55:27 -05:00
|
|
|
}
|
2014-02-07 11:48:14 -05:00
|
|
|
|
2014-06-26 07:03:23 -04:00
|
|
|
// 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) {
|
2014-09-15 23:30:10 -04:00
|
|
|
if alphaRegexp.FindString(val) == "" {
|
2014-02-07 11:48:14 -05:00
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|
2014-09-15 23:30:10 -04:00
|
|
|
ns := domainRegexp.FindSubmatch([]byte(val))
|
2014-02-07 11:48:14 -05:00
|
|
|
if len(ns) > 0 {
|
|
|
|
return string(ns[1]), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|
2014-07-18 14:48:19 -04:00
|
|
|
|
2014-09-13 00:35:59 -04:00
|
|
|
func ValidateExtraHost(val string) (string, error) {
|
|
|
|
arr := strings.Split(val, ":")
|
|
|
|
if len(arr) != 2 || len(arr[0]) == 0 {
|
|
|
|
return "", fmt.Errorf("bad format for add-host: %s", val)
|
|
|
|
}
|
|
|
|
if _, err := ValidateIPAddress(arr[1]); err != nil {
|
|
|
|
return "", fmt.Errorf("bad format for add-host: %s", val)
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2014-07-18 14:48:19 -04:00
|
|
|
// Validates an HTTP(S) registry mirror
|
|
|
|
func ValidateMirror(val string) (string, error) {
|
|
|
|
uri, err := url.Parse(val)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%s is not a valid URI", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri.Scheme != "http" && uri.Scheme != "https" {
|
|
|
|
return "", fmt.Errorf("Unsupported scheme %s", uri.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri.Path != "" || uri.RawQuery != "" || uri.Fragment != "" {
|
|
|
|
return "", fmt.Errorf("Unsupported path/query/fragment at end of the URI")
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s://%s/v1/", uri.Scheme, uri.Host), nil
|
|
|
|
}
|