github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/opts/env.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 "os" 6 "runtime" 7 "strings" 8 ) 9 10 // ValidateEnv validates an environment variable and returns it. 11 // If no value is specified, it returns the current value using os.Getenv. 12 // 13 // As on ParseEnvFile and related to #16585, environment variable names 14 // are not validate what so ever, it's up to application inside docker 15 // to validate them or not. 16 // 17 // The only validation here is to check if name is empty, per #25099 18 func ValidateEnv(val string) (string, error) { 19 arr := strings.Split(val, "=") 20 if arr[0] == "" { 21 return "", fmt.Errorf("invalid environment variable: %s", val) 22 } 23 if len(arr) > 1 { 24 return val, nil 25 } 26 if !doesEnvExist(val) { 27 return val, nil 28 } 29 return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil 30 } 31 32 func doesEnvExist(name string) bool { 33 for _, entry := range os.Environ() { 34 parts := strings.SplitN(entry, "=", 2) 35 if runtime.GOOS == "windows" { 36 // Environment variable are case-insensitive on Windows. PaTh, path and PATH are equivalent. 37 if strings.EqualFold(parts[0], name) { 38 return true 39 } 40 } 41 if parts[0] == name { 42 return true 43 } 44 } 45 return false 46 }