2014-02-12 02:46:55 +00:00
|
|
|
package opts
|
2013-11-26 17:46:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-02-12 02:46:55 +00:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-11-26 17:46:06 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-11-22 17:55:27 +00:00
|
|
|
"regexp"
|
2013-11-26 17:46:06 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListOpts type
|
|
|
|
type ListOpts struct {
|
|
|
|
values []string
|
|
|
|
validator ValidatorFctType
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewListOpts(validator ValidatorFctType) ListOpts {
|
|
|
|
return ListOpts{
|
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *ListOpts) String() string {
|
|
|
|
return fmt.Sprintf("%v", []string(opts.values))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
opts.values = append(opts.values, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete remove the given element from the slice.
|
|
|
|
func (opts *ListOpts) Delete(key string) {
|
|
|
|
for i, k := range opts.values {
|
|
|
|
if k == key {
|
|
|
|
opts.values = append(opts.values[:i], opts.values[i+1:]...)
|
|
|
|
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{})
|
|
|
|
for _, k := range opts.values {
|
|
|
|
ret[k] = struct{}{}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll returns the values' slice.
|
|
|
|
// FIXME: Can we remove this?
|
|
|
|
func (opts *ListOpts) GetAll() []string {
|
|
|
|
return opts.values
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get checks the existence of the given key.
|
|
|
|
func (opts *ListOpts) Get(key string) bool {
|
|
|
|
for _, k := range opts.values {
|
|
|
|
if k == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the amount of element in the slice.
|
|
|
|
func (opts *ListOpts) Len() int {
|
|
|
|
return len(opts.values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validators
|
|
|
|
type ValidatorFctType func(val string) (string, error)
|
|
|
|
|
|
|
|
func ValidateAttach(val string) (string, error) {
|
|
|
|
if val != "stdin" && val != "stdout" && val != "stderr" {
|
|
|
|
return val, fmt.Errorf("Unsupported stream name: %s", val)
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateLink(val string) (string, error) {
|
2014-03-10 21:06:27 +00:00
|
|
|
if _, err := utils.PartParser("name:alias", val); err != nil {
|
2013-11-26 17:46:06 +00:00
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidatePath(val string) (string, error) {
|
|
|
|
var containerPath string
|
|
|
|
|
2013-11-28 20:24:04 +00:00
|
|
|
if strings.Count(val, ":") > 2 {
|
|
|
|
return val, fmt.Errorf("bad format for volumes: %s", val)
|
|
|
|
}
|
|
|
|
|
2013-11-26 17:46:06 +00: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
|
|
|
|
}
|
|
|
|
|
2013-11-22 17:55:27 +00:00
|
|
|
func ValidateIp4Address(val string) (string, error) {
|
|
|
|
re := regexp.MustCompile(`^(([0-9]+\.){3}([0-9]+))\s*$`)
|
|
|
|
var ns = re.FindSubmatch([]byte(val))
|
|
|
|
if len(ns) > 0 {
|
|
|
|
return string(ns[1]), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%s is not an ip4 address", val)
|
|
|
|
}
|
2014-02-07 16:48:14 +00:00
|
|
|
|
|
|
|
func ValidateDomain(val string) (string, error) {
|
|
|
|
alpha := regexp.MustCompile(`[a-zA-Z]`)
|
|
|
|
if alpha.FindString(val) == "" {
|
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|
|
|
|
re := 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-03-19 20:00:46 +00:00
|
|
|
ns := re.FindSubmatch([]byte(val))
|
2014-02-07 16:48:14 +00:00
|
|
|
if len(ns) > 0 {
|
|
|
|
return string(ns[1]), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|