2018-02-05 16:05:59 -05:00
|
|
|
package opts // import "github.com/docker/docker/opts"
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
2017-07-19 10:20:13 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-12-23 14:09:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateEnv validates an environment variable and returns it.
|
2020-08-14 10:54:08 -04:00
|
|
|
// If no value is specified, it obtains its value from the current environment
|
2016-12-23 14:09:12 -05:00
|
|
|
//
|
|
|
|
// As on ParseEnvFile and related to #16585, environment variable names
|
2020-08-14 10:54:08 -04:00
|
|
|
// are not validate whatsoever, it's up to application inside docker
|
2016-12-23 14:09:12 -05:00
|
|
|
// to validate them or not.
|
|
|
|
//
|
|
|
|
// The only validation here is to check if name is empty, per #25099
|
|
|
|
func ValidateEnv(val string) (string, error) {
|
2020-08-14 10:54:08 -04:00
|
|
|
arr := strings.SplitN(val, "=", 2)
|
2016-12-23 14:09:12 -05:00
|
|
|
if arr[0] == "" {
|
2020-08-14 10:54:08 -04:00
|
|
|
return "", errors.New("invalid environment variable: " + val)
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
|
|
|
if len(arr) > 1 {
|
|
|
|
return val, nil
|
|
|
|
}
|
2020-08-14 10:54:08 -04:00
|
|
|
if envVal, ok := os.LookupEnv(arr[0]); ok {
|
|
|
|
return arr[0] + "=" + envVal, nil
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2020-08-14 10:54:08 -04:00
|
|
|
return val, nil
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|