|
|
|
|
@@ -27,18 +27,19 @@ var (
|
|
|
|
|
DefaultUnixSocket = "/var/run/docker.sock"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ListOpts type that hold a list of values and a validation function.
|
|
|
|
|
// ListOpts holds a list of values and a validation function.
|
|
|
|
|
type ListOpts struct {
|
|
|
|
|
values *[]string
|
|
|
|
|
validator ValidatorFctType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewListOpts Create a new ListOpts with the specified validator.
|
|
|
|
|
// NewListOpts creates a new ListOpts with the specified validator.
|
|
|
|
|
func NewListOpts(validator ValidatorFctType) ListOpts {
|
|
|
|
|
var values []string
|
|
|
|
|
return *NewListOptsRef(&values, validator)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewListOptsRef creates a new ListOpts with the specified values and validator.
|
|
|
|
|
func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
|
|
|
|
|
return &ListOpts{
|
|
|
|
|
values: values,
|
|
|
|
|
@@ -64,7 +65,7 @@ func (opts *ListOpts) Set(value string) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete remove the given element from the slice.
|
|
|
|
|
// Delete removes the specified element from the slice.
|
|
|
|
|
func (opts *ListOpts) Delete(key string) {
|
|
|
|
|
for i, k := range *opts.values {
|
|
|
|
|
if k == key {
|
|
|
|
|
@@ -85,13 +86,13 @@ func (opts *ListOpts) GetMap() map[string]struct{} {
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAll returns the values' slice.
|
|
|
|
|
// GetAll returns the values of slice.
|
|
|
|
|
// FIXME: Can we remove this?
|
|
|
|
|
func (opts *ListOpts) GetAll() []string {
|
|
|
|
|
return (*opts.values)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get checks the existence of the given key.
|
|
|
|
|
// Get checks the existence of the specified key.
|
|
|
|
|
func (opts *ListOpts) Get(key string) bool {
|
|
|
|
|
for _, k := range *opts.values {
|
|
|
|
|
if k == key {
|
|
|
|
|
@@ -106,7 +107,7 @@ func (opts *ListOpts) Len() int {
|
|
|
|
|
return len((*opts.values))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//MapOpts type that holds a map of values and a validation function.
|
|
|
|
|
//MapOpts holds a map of values and a validation function.
|
|
|
|
|
type MapOpts struct {
|
|
|
|
|
values map[string]string
|
|
|
|
|
validator ValidatorFctType
|
|
|
|
|
@@ -131,6 +132,7 @@ func (opts *MapOpts) Set(value string) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAll returns the values of MapOpts as a map.
|
|
|
|
|
func (opts *MapOpts) GetAll() map[string]string {
|
|
|
|
|
return opts.values
|
|
|
|
|
}
|
|
|
|
|
@@ -139,6 +141,7 @@ func (opts *MapOpts) String() string {
|
|
|
|
|
return fmt.Sprintf("%v", map[string]string((opts.values)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewMapOpts creates a new MapOpts with the specified map of values and a validator.
|
|
|
|
|
func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
|
|
|
|
|
if values == nil {
|
|
|
|
|
values = make(map[string]string)
|
|
|
|
|
@@ -149,13 +152,13 @@ func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidatorFctType validator that return a validate string and/or an error
|
|
|
|
|
// ValidatorFctType defines a validator function that returns a validated string and/or an error.
|
|
|
|
|
type ValidatorFctType func(val string) (string, error)
|
|
|
|
|
|
|
|
|
|
// ValidatorFctListType validator that return a validate list of string and/or an error
|
|
|
|
|
// ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
|
|
|
|
|
type ValidatorFctListType func(val string) ([]string, error)
|
|
|
|
|
|
|
|
|
|
// ValidateAttach Validates that the specified string is a valid attach option.
|
|
|
|
|
// ValidateAttach validates that the specified string is a valid attach option.
|
|
|
|
|
func ValidateAttach(val string) (string, error) {
|
|
|
|
|
s := strings.ToLower(val)
|
|
|
|
|
for _, str := range []string{"stdin", "stdout", "stderr"} {
|
|
|
|
|
@@ -166,7 +169,7 @@ func ValidateAttach(val string) (string, error) {
|
|
|
|
|
return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateLink Validates that the specified string has a valid link format (containerName:alias).
|
|
|
|
|
// ValidateLink validates that the specified string has a valid link format (containerName:alias).
|
|
|
|
|
func ValidateLink(val string) (string, error) {
|
|
|
|
|
if _, _, err := parsers.ParseLink(val); err != nil {
|
|
|
|
|
return val, err
|
|
|
|
|
@@ -194,18 +197,18 @@ func ValidDeviceMode(mode string) bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateDevice Validate a path for devices
|
|
|
|
|
// ValidateDevice validates a path for devices
|
|
|
|
|
// It will make sure 'val' is in the form:
|
|
|
|
|
// [host-dir:]container-path[:mode]
|
|
|
|
|
// It will also validate the device mode.
|
|
|
|
|
// It also validates the device mode.
|
|
|
|
|
func ValidateDevice(val string) (string, error) {
|
|
|
|
|
return validatePath(val, ValidDeviceMode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidatePath Validate a path for volumes
|
|
|
|
|
// ValidatePath validates a path for volumes
|
|
|
|
|
// It will make sure 'val' is in the form:
|
|
|
|
|
// [host-dir:]container-path[:rw|ro]
|
|
|
|
|
// It will also validate the mount mode.
|
|
|
|
|
// It also validates the mount mode.
|
|
|
|
|
func ValidatePath(val string) (string, error) {
|
|
|
|
|
return validatePath(val, volume.ValidMountMode)
|
|
|
|
|
}
|
|
|
|
|
@@ -250,8 +253,8 @@ func validatePath(val string, validator func(string) bool) (string, error) {
|
|
|
|
|
return val, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateEnv Validate an environment variable and returns it
|
|
|
|
|
// It will use EnvironmentVariableRegexp to ensure the name of the environment variable is valid.
|
|
|
|
|
// ValidateEnv validates an environment variable and returns it.
|
|
|
|
|
// It uses EnvironmentVariableRegexp to ensure the name of the environment variable is valid.
|
|
|
|
|
// If no value is specified, it returns the current value using os.Getenv.
|
|
|
|
|
func ValidateEnv(val string) (string, error) {
|
|
|
|
|
arr := strings.Split(val, "=")
|
|
|
|
|
@@ -267,7 +270,7 @@ func ValidateEnv(val string) (string, error) {
|
|
|
|
|
return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateIPAddress Validates an Ip address
|
|
|
|
|
// ValidateIPAddress validates an Ip address.
|
|
|
|
|
func ValidateIPAddress(val string) (string, error) {
|
|
|
|
|
var ip = net.ParseIP(strings.TrimSpace(val))
|
|
|
|
|
if ip != nil {
|
|
|
|
|
@@ -276,7 +279,7 @@ func ValidateIPAddress(val string) (string, error) {
|
|
|
|
|
return "", fmt.Errorf("%s is not an ip address", val)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateMACAddress Validates a MAC address
|
|
|
|
|
// ValidateMACAddress validates a MAC address.
|
|
|
|
|
func ValidateMACAddress(val string) (string, error) {
|
|
|
|
|
_, err := net.ParseMAC(strings.TrimSpace(val))
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -285,8 +288,8 @@ func ValidateMACAddress(val string) (string, error) {
|
|
|
|
|
return val, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateDNSSearch Validates domain for resolvconf search configuration.
|
|
|
|
|
// A zero length domain is represented by .
|
|
|
|
|
// ValidateDNSSearch validates domain for resolvconf search configuration.
|
|
|
|
|
// A zero length domain is represented by a dot (.).
|
|
|
|
|
func ValidateDNSSearch(val string) (string, error) {
|
|
|
|
|
if val = strings.Trim(val, " "); val == "." {
|
|
|
|
|
return val, nil
|
|
|
|
|
@@ -305,8 +308,8 @@ func validateDomain(val string) (string, error) {
|
|
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateExtraHost Validate that the given string is a valid extrahost and returns it
|
|
|
|
|
// ExtraHost are in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6)
|
|
|
|
|
// ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
|
|
|
|
|
// ExtraHost are in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6).
|
|
|
|
|
func ValidateExtraHost(val string) (string, error) {
|
|
|
|
|
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
|
|
|
|
arr := strings.SplitN(val, ":", 2)
|
|
|
|
|
@@ -319,8 +322,8 @@ func ValidateExtraHost(val string) (string, error) {
|
|
|
|
|
return val, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateLabel Validate that the given string is a valid label, and returns it
|
|
|
|
|
// Labels are in the form on key=value
|
|
|
|
|
// ValidateLabel validates that the specified string is a valid label, and returns it.
|
|
|
|
|
// Labels are in the form on key=value.
|
|
|
|
|
func ValidateLabel(val string) (string, error) {
|
|
|
|
|
if strings.Count(val, "=") < 1 {
|
|
|
|
|
return "", fmt.Errorf("bad attribute format: %s", val)
|
|
|
|
|
@@ -328,7 +331,7 @@ func ValidateLabel(val string) (string, error) {
|
|
|
|
|
return val, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ValidateHost Validate that the given string is a valid host and returns it
|
|
|
|
|
// ValidateHost validates that the specified string is a valid host and returns it.
|
|
|
|
|
func ValidateHost(val string) (string, error) {
|
|
|
|
|
host, err := parsers.ParseHost(DefaultHTTPHost, DefaultUnixSocket, val)
|
|
|
|
|
if err != nil {
|
|
|
|
|
|