github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/runconfig/opts/opts.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os"
     7  	"strings"
     8  
     9  	fopts "github.com/docker/docker/opts"
    10  )
    11  
    12  // ValidateAttach validates that the specified string is a valid attach option.
    13  func ValidateAttach(val string) (string, error) {
    14  	s := strings.ToLower(val)
    15  	for _, str := range []string{"stdin", "stdout", "stderr"} {
    16  		if s == str {
    17  			return s, nil
    18  		}
    19  	}
    20  	return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR")
    21  }
    22  
    23  // ValidateEnv validates an environment variable and returns it.
    24  // If no value is specified, it returns the current value using os.Getenv.
    25  //
    26  // As on ParseEnvFile and related to #16585, environment variable names
    27  // are not validate what so ever, it's up to application inside docker
    28  // to validate them or not.
    29  func ValidateEnv(val string) (string, error) {
    30  	arr := strings.Split(val, "=")
    31  	if len(arr) > 1 {
    32  		return val, nil
    33  	}
    34  	if !doesEnvExist(val) {
    35  		return val, nil
    36  	}
    37  	return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
    38  }
    39  
    40  func doesEnvExist(name string) bool {
    41  	for _, entry := range os.Environ() {
    42  		parts := strings.SplitN(entry, "=", 2)
    43  		if parts[0] == name {
    44  			return true
    45  		}
    46  	}
    47  	return false
    48  }
    49  
    50  // ValidateArg validates a build-arg variable and returns it.
    51  // Build-arg is in the form of <varname>=<value> where <varname> is required.
    52  func ValidateArg(val string) (string, error) {
    53  	arr := strings.Split(val, "=")
    54  	if len(arr) > 1 && isNotEmpty(arr[0]) {
    55  		return val, nil
    56  	}
    57  
    58  	return "", fmt.Errorf("bad format for build-arg: %s", val)
    59  }
    60  
    61  func isNotEmpty(val string) bool {
    62  	return len(val) > 0
    63  }
    64  
    65  // ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
    66  // ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6).
    67  func ValidateExtraHost(val string) (string, error) {
    68  	// allow for IPv6 addresses in extra hosts by only splitting on first ":"
    69  	arr := strings.SplitN(val, ":", 2)
    70  	if len(arr) != 2 || len(arr[0]) == 0 {
    71  		return "", fmt.Errorf("bad format for add-host: %q", val)
    72  	}
    73  	if _, err := fopts.ValidateIPAddress(arr[1]); err != nil {
    74  		return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
    75  	}
    76  	return val, nil
    77  }
    78  
    79  // ValidateMACAddress validates a MAC address.
    80  func ValidateMACAddress(val string) (string, error) {
    81  	_, err := net.ParseMAC(strings.TrimSpace(val))
    82  	if err != nil {
    83  		return "", err
    84  	}
    85  	return val, nil
    86  }