1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/opts/opts.go

219 lines
5.3 KiB
Go
Raw Normal View History

package opts
2013-11-26 12:46:06 -05:00
import (
"fmt"
"net"
2013-11-26 12:46:06 -05:00
"os"
"path"
2013-11-22 12:55:27 -05:00
"regexp"
2013-11-26 12:46:06 -05:00
"strings"
"github.com/docker/docker/api"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/parsers"
2013-11-26 12:46:06 -05: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*$`)
)
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)
}
func LabelListVar(values *[]string, names []string, usage string) {
flag.Var(newListOptsRef(values, ValidateLabel), names, usage)
}
2013-11-26 12:46:06 -05:00
// ListOpts type
type ListOpts struct {
values *[]string
2013-11-26 12:46:06 -05:00
validator ValidatorFctType
}
func NewListOpts(validator ValidatorFctType) ListOpts {
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 {
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
}
(*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) {
for i, k := range *opts.values {
2013-11-26 12:46:06 -05:00
if k == key {
(*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{})
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 {
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 {
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 {
return len((*opts.values))
2013-11-26 12:46:06 -05:00
}
// Validators
type ValidatorFctType func(val string) (string, error)
Deprecating ResolveRepositoryName Passing RepositoryInfo to ResolveAuthConfig, pullRepository, and pushRepository Moving --registry-mirror configuration to registry config Created resolve_repository job Repo names with 'index.docker.io' or 'docker.io' are now synonymous with omitting an index name. Adding test for RepositoryInfo Adding tests for opts.StringSetOpts and registry.ValidateMirror Fixing search term use of repoInfo Adding integration tests for registry mirror configuration Normalizing LookupImage image name to match LocalName parsing rules Normalizing repository LocalName to avoid multiple references to an official image Removing errorOut use in tests Removing TODO comment gofmt changes golint comments cleanup. renaming RegistryOptions => registry.Options, and RegistryServiceConfig => registry.ServiceConfig Splitting out builtins.Registry and registry.NewService calls Stray whitespace cleanup Moving integration tests for Mirrors and InsecureRegistries into TestNewIndexInfo unit test Factoring out ValidateRepositoryName from NewRepositoryInfo Removing unused IndexServerURL Allowing json marshaling of ServiceConfig. Exposing ServiceConfig in /info Switching to CamelCase for json marshaling PR cleanup; removing 'Is' prefix from boolean members. Removing unneeded json tags. Removing non-cleanup related fix for 'localhost:[port]' in splitReposName Merge fixes for gh9735 Fixing integration test Reapplying #9754 Adding comment on config.IndexConfigs use from isSecureIndex Remove unused error return value from isSecureIndex Signed-off-by: Don Kjer <don.kjer@gmail.com> Adding back comment in isSecureIndex Signed-off-by: Don Kjer <don.kjer@gmail.com>
2014-10-06 21:54:52 -04:00
type ValidatorFctListType func(val string) ([]string, error)
2013-11-26 12:46:06 -05:00
func ValidateAttach(val string) (string, error) {
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
}
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) {
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 = path.Clean(splited[0])
2013-11-26 12:46:06 -05:00
} else {
containerPath = splited[1]
val = fmt.Sprintf("%s:%s", splited[0], path.Clean(splited[1]))
2013-11-26 12:46:06 -05:00
}
if !path.IsAbs(containerPath) {
2013-11-26 12:46:06 -05:00
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
}
func ValidateIPAddress(val string) (string, error) {
var ip = net.ParseIP(strings.TrimSpace(val))
if ip != nil {
return ip.String(), nil
2013-11-22 12:55:27 -05:00
}
return "", fmt.Errorf("%s is not an ip address", val)
2013-11-22 12:55:27 -05: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) {
if alphaRegexp.FindString(val) == "" {
return "", fmt.Errorf("%s is not a valid domain", val)
}
ns := domainRegexp.FindSubmatch([]byte(val))
if len(ns) > 0 {
return string(ns[1]), nil
}
return "", fmt.Errorf("%s is not a valid domain", val)
}
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
}
func ValidateLabel(val string) (string, error) {
if strings.Count(val, "=") != 1 {
return "", fmt.Errorf("bad attribute format: %s", val)
}
return val, nil
}