2014-02-12 02:46:55 +00:00
package opts
2013-11-26 17:46:06 +00:00
import (
"fmt"
2014-06-13 12:02:12 +00:00
"net"
2013-11-26 17:46:06 +00:00
"os"
2013-11-22 17:55:27 +00:00
"regexp"
2015-06-12 00:34:20 +00:00
"strconv"
2013-11-26 17:46:06 +00:00
"strings"
2014-07-09 21:47:55 +00:00
2015-06-12 00:34:20 +00:00
"github.com/docker/docker/pkg/blkiodev"
2015-12-16 17:26:49 +00:00
"github.com/docker/go-units"
2013-11-26 17:46:06 +00:00
)
2014-09-16 03:30:10 +00:00
var (
2015-07-12 08:33:30 +00:00
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-09-16 03:30:10 +00:00
)
2015-08-27 07:33:21 +00:00
// ListOpts holds a list of values and a validation function.
2013-11-26 17:46:06 +00:00
type ListOpts struct {
2014-08-10 01:13:44 +00:00
values * [ ] string
2013-11-26 17:46:06 +00:00
validator ValidatorFctType
}
2015-08-27 07:33:21 +00:00
// NewListOpts creates a new ListOpts with the specified validator.
2013-11-26 17:46:06 +00:00
func NewListOpts ( validator ValidatorFctType ) ListOpts {
2014-08-10 01:13:44 +00:00
var values [ ] string
2015-05-05 04:18:28 +00:00
return * NewListOptsRef ( & values , validator )
2014-08-10 01:13:44 +00:00
}
2015-08-27 07:33:21 +00:00
// NewListOptsRef creates a new ListOpts with the specified values and validator.
2015-05-05 04:18:28 +00:00
func NewListOptsRef ( values * [ ] string , validator ValidatorFctType ) * ListOpts {
2014-08-10 01:13:44 +00:00
return & ListOpts {
values : values ,
2013-11-26 17:46:06 +00:00
validator : validator ,
}
}
func ( opts * ListOpts ) String ( ) string {
2014-08-10 01:13:44 +00:00
return fmt . Sprintf ( "%v" , [ ] string ( ( * opts . values ) ) )
2013-11-26 17:46:06 +00: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-10 01:13:44 +00:00
( * opts . values ) = append ( ( * opts . values ) , value )
2013-11-26 17:46:06 +00:00
return nil
}
2015-08-27 07:33:21 +00:00
// Delete removes the specified element from the slice.
2013-11-26 17:46:06 +00:00
func ( opts * ListOpts ) Delete ( key string ) {
2014-08-10 01:13:44 +00:00
for i , k := range * opts . values {
2013-11-26 17:46:06 +00:00
if k == key {
2014-08-10 01:13:44 +00:00
( * opts . values ) = append ( ( * opts . values ) [ : i ] , ( * opts . values ) [ i + 1 : ] ... )
2013-11-26 17:46:06 +00:00
return
}
}
}
// GetMap returns the content of values in a map in order to avoid
// duplicates.
func ( opts * ListOpts ) GetMap ( ) map [ string ] struct { } {
ret := make ( map [ string ] struct { } )
2014-08-10 01:13:44 +00:00
for _ , k := range * opts . values {
2013-11-26 17:46:06 +00:00
ret [ k ] = struct { } { }
}
return ret
}
2015-08-27 07:33:21 +00:00
// GetAll returns the values of slice.
2013-11-26 17:46:06 +00:00
func ( opts * ListOpts ) GetAll ( ) [ ] string {
2014-08-10 01:13:44 +00:00
return ( * opts . values )
2013-11-26 17:46:06 +00:00
}
2015-11-06 22:22:48 +00:00
// GetAllOrEmpty returns the values of the slice
// or an empty slice when there are no values.
func ( opts * ListOpts ) GetAllOrEmpty ( ) [ ] string {
v := * opts . values
if v == nil {
return make ( [ ] string , 0 )
}
return v
}
2015-08-27 07:33:21 +00:00
// Get checks the existence of the specified key.
2013-11-26 17:46:06 +00:00
func ( opts * ListOpts ) Get ( key string ) bool {
2014-08-10 01:13:44 +00:00
for _ , k := range * opts . values {
2013-11-26 17:46:06 +00:00
if k == key {
return true
}
}
return false
}
// Len returns the amount of element in the slice.
func ( opts * ListOpts ) Len ( ) int {
2014-08-10 01:13:44 +00:00
return len ( ( * opts . values ) )
2013-11-26 17:46:06 +00:00
}
2015-08-27 07:33:21 +00:00
//MapOpts holds a map of values and a validation function.
2015-05-04 21:39:48 +00:00
type MapOpts struct {
values map [ string ] string
validator ValidatorFctType
}
2015-07-12 08:33:30 +00:00
// Set validates if needed the input value and add it to the
// internal map, by splitting on '='.
2015-05-04 21:39:48 +00:00
func ( opts * MapOpts ) Set ( value string ) error {
if opts . validator != nil {
v , err := opts . validator ( value )
if err != nil {
return err
}
value = v
}
vals := strings . SplitN ( value , "=" , 2 )
if len ( vals ) == 1 {
( opts . values ) [ vals [ 0 ] ] = ""
} else {
( opts . values ) [ vals [ 0 ] ] = vals [ 1 ]
}
return nil
}
2015-08-27 07:33:21 +00:00
// GetAll returns the values of MapOpts as a map.
2015-06-12 13:25:32 +00:00
func ( opts * MapOpts ) GetAll ( ) map [ string ] string {
return opts . values
}
2015-05-04 21:39:48 +00:00
func ( opts * MapOpts ) String ( ) string {
return fmt . Sprintf ( "%v" , map [ string ] string ( ( opts . values ) ) )
}
2015-08-27 07:33:21 +00:00
// NewMapOpts creates a new MapOpts with the specified map of values and a validator.
2015-05-05 04:18:28 +00:00
func NewMapOpts ( values map [ string ] string , validator ValidatorFctType ) * MapOpts {
if values == nil {
values = make ( map [ string ] string )
}
2015-05-04 21:39:48 +00:00
return & MapOpts {
values : values ,
validator : validator ,
}
}
2015-08-27 07:33:21 +00:00
// ValidatorFctType defines a validator function that returns a validated string and/or an error.
2013-11-26 17:46:06 +00:00
type ValidatorFctType func ( val string ) ( string , error )
2015-07-12 08:33:30 +00:00
2015-06-12 00:34:20 +00:00
// ValidatorWeightFctType defines a validator function that returns a validated struct and/or an error.
type ValidatorWeightFctType func ( val string ) ( * blkiodev . WeightDevice , error )
2015-07-08 11:06:48 +00:00
// ValidatorThrottleFctType defines a validator function that returns a validated struct and/or an error.
type ValidatorThrottleFctType func ( val string ) ( * blkiodev . ThrottleDevice , error )
2015-08-27 07:33:21 +00:00
// ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
2014-10-07 01:54:52 +00:00
type ValidatorFctListType func ( val string ) ( [ ] string , error )
2013-11-26 17:46:06 +00:00
2015-08-27 07:33:21 +00:00
// ValidateAttach validates that the specified string is a valid attach option.
2013-11-26 17:46:06 +00:00
func ValidateAttach ( val string ) ( string , error ) {
2014-07-30 14:20:52 +00:00
s := strings . ToLower ( val )
for _ , str := range [ ] string { "stdin" , "stdout" , "stderr" } {
if s == str {
return s , nil
}
2013-11-26 17:46:06 +00:00
}
2015-07-12 08:33:30 +00:00
return val , fmt . Errorf ( "valid streams are STDIN, STDOUT and STDERR" )
2013-11-26 17:46:06 +00:00
}
2015-06-12 00:34:20 +00:00
// ValidateWeightDevice validates that the specified string has a valid device-weight format.
func ValidateWeightDevice ( val string ) ( * blkiodev . WeightDevice , error ) {
split := strings . SplitN ( val , ":" , 2 )
if len ( split ) != 2 {
return nil , fmt . Errorf ( "bad format: %s" , val )
}
if ! strings . HasPrefix ( split [ 0 ] , "/dev/" ) {
return nil , fmt . Errorf ( "bad format for device path: %s" , val )
}
weight , err := strconv . ParseUint ( split [ 1 ] , 10 , 0 )
if err != nil {
return nil , fmt . Errorf ( "invalid weight for device: %s" , val )
}
if weight > 0 && ( weight < 10 || weight > 1000 ) {
return nil , fmt . Errorf ( "invalid weight for device: %s" , val )
}
return & blkiodev . WeightDevice {
Path : split [ 0 ] ,
Weight : uint16 ( weight ) ,
} , nil
}
2015-07-08 11:06:48 +00:00
// ValidateThrottleBpsDevice validates that the specified string has a valid device-rate format.
func ValidateThrottleBpsDevice ( val string ) ( * blkiodev . ThrottleDevice , error ) {
split := strings . SplitN ( val , ":" , 2 )
if len ( split ) != 2 {
return nil , fmt . Errorf ( "bad format: %s" , val )
}
if ! strings . HasPrefix ( split [ 0 ] , "/dev/" ) {
return nil , fmt . Errorf ( "bad format for device path: %s" , val )
}
rate , err := units . RAMInBytes ( split [ 1 ] )
if err != nil {
return nil , fmt . Errorf ( "invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb" , val )
}
if rate < 0 {
return nil , fmt . Errorf ( "invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb" , val )
}
return & blkiodev . ThrottleDevice {
Path : split [ 0 ] ,
Rate : uint64 ( rate ) ,
} , nil
}
2015-07-08 11:06:48 +00:00
// ValidateThrottleIOpsDevice validates that the specified string has a valid device-rate format.
func ValidateThrottleIOpsDevice ( val string ) ( * blkiodev . ThrottleDevice , error ) {
split := strings . SplitN ( val , ":" , 2 )
if len ( split ) != 2 {
return nil , fmt . Errorf ( "bad format: %s" , val )
}
if ! strings . HasPrefix ( split [ 0 ] , "/dev/" ) {
return nil , fmt . Errorf ( "bad format for device path: %s" , val )
}
rate , err := strconv . ParseUint ( split [ 1 ] , 10 , 64 )
if err != nil {
return nil , fmt . Errorf ( "invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer" , val )
}
if rate < 0 {
return nil , fmt . Errorf ( "invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer" , val )
}
return & blkiodev . ThrottleDevice {
Path : split [ 0 ] ,
Rate : uint64 ( rate ) ,
} , nil
}
2015-08-27 07:33:21 +00:00
// ValidateEnv validates an environment variable and returns it.
2015-07-12 08:33:30 +00:00
// If no value is specified, it returns the current value using os.Getenv.
2015-09-28 18:26:20 +00:00
//
// As on ParseEnvFile and related to #16585, environment variable names
// are not validate what so ever, it's up to application inside docker
// to validate them or not.
2013-11-26 17:46:06 +00:00
func ValidateEnv ( val string ) ( string , error ) {
arr := strings . Split ( val , "=" )
if len ( arr ) > 1 {
return val , nil
}
2015-04-13 14:17:14 +00:00
if ! doesEnvExist ( val ) {
2015-01-16 20:57:08 +00:00
return val , nil
}
2013-11-26 17:46:06 +00:00
return fmt . Sprintf ( "%s=%s" , val , os . Getenv ( val ) ) , nil
}
2015-08-27 07:33:21 +00:00
// ValidateIPAddress validates an Ip address.
2014-07-09 21:47:55 +00:00
func ValidateIPAddress ( val string ) ( string , error ) {
2014-06-13 12:02:12 +00:00
var ip = net . ParseIP ( strings . TrimSpace ( val ) )
if ip != nil {
return ip . String ( ) , nil
2013-11-22 17:55:27 +00:00
}
2014-06-13 12:02:12 +00:00
return "" , fmt . Errorf ( "%s is not an ip address" , val )
2013-11-22 17:55:27 +00:00
}
2014-02-07 16:48:14 +00:00
2015-08-27 07:33:21 +00:00
// ValidateMACAddress validates a MAC address.
2015-02-27 15:27:12 +00:00
func ValidateMACAddress ( val string ) ( string , error ) {
_ , err := net . ParseMAC ( strings . TrimSpace ( val ) )
if err != nil {
return "" , err
}
2015-03-23 19:21:37 +00:00
return val , nil
2015-02-27 15:27:12 +00:00
}
2015-08-27 07:33:21 +00:00
// ValidateDNSSearch validates domain for resolvconf search configuration.
// A zero length domain is represented by a dot (.).
2015-07-12 08:33:30 +00:00
func ValidateDNSSearch ( val string ) ( string , error ) {
2014-06-26 11:03:23 +00:00
if val = strings . Trim ( val , " " ) ; val == "." {
return val , nil
}
return validateDomain ( val )
}
func validateDomain ( val string ) ( string , error ) {
2014-09-16 03:30:10 +00:00
if alphaRegexp . FindString ( val ) == "" {
2014-02-07 16:48:14 +00:00
return "" , fmt . Errorf ( "%s is not a valid domain" , val )
}
2014-09-16 03:30:10 +00:00
ns := domainRegexp . FindSubmatch ( [ ] byte ( val ) )
2014-11-25 11:45:20 +00:00
if len ( ns ) > 0 && len ( ns [ 1 ] ) < 255 {
2014-02-07 16:48:14 +00:00
return string ( ns [ 1 ] ) , nil
}
return "" , fmt . Errorf ( "%s is not a valid domain" , val )
}
2014-07-18 18:48:19 +00:00
2015-08-27 07:33:21 +00:00
// 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).
2014-09-13 04:35:59 +00:00
func ValidateExtraHost ( val string ) ( string , error ) {
2015-02-04 15:20:28 +00:00
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
arr := strings . SplitN ( val , ":" , 2 )
2014-09-13 04:35:59 +00:00
if len ( arr ) != 2 || len ( arr [ 0 ] ) == 0 {
2015-02-09 14:59:05 +00:00
return "" , fmt . Errorf ( "bad format for add-host: %q" , val )
2014-09-13 04:35:59 +00:00
}
if _ , err := ValidateIPAddress ( arr [ 1 ] ) ; err != nil {
2015-02-09 14:59:05 +00:00
return "" , fmt . Errorf ( "invalid IP address in add-host: %q" , arr [ 1 ] )
2014-09-13 04:35:59 +00:00
}
return val , nil
}
2015-08-27 07:33:21 +00:00
// ValidateLabel validates that the specified string is a valid label, and returns it.
// Labels are in the form on key=value.
2014-11-20 18:36:05 +00:00
func ValidateLabel ( val string ) ( string , error ) {
2015-07-12 08:33:30 +00:00
if strings . Count ( val , "=" ) < 1 {
2014-11-20 18:36:05 +00:00
return "" , fmt . Errorf ( "bad attribute format: %s" , val )
}
return val , nil
}
2015-04-13 14:17:14 +00:00
func doesEnvExist ( name string ) bool {
for _ , entry := range os . Environ ( ) {
parts := strings . SplitN ( entry , "=" , 2 )
if parts [ 0 ] == name {
return true
}
}
return false
}