2018-02-05 21:05:59 +00:00
|
|
|
package opts // import "github.com/docker/docker/opts"
|
2016-12-23 19:09:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
2017-07-19 14:20:13 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-12-23 19:09:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateEnv validates an environment variable and returns it.
|
2020-08-14 14:54:08 +00:00
|
|
|
// If no value is specified, it obtains its value from the current environment
|
2016-12-23 19:09:12 +00:00
|
|
|
//
|
|
|
|
// As on ParseEnvFile and related to #16585, environment variable names
|
2020-08-14 14:54:08 +00:00
|
|
|
// are not validate whatsoever, it's up to application inside docker
|
2016-12-23 19:09:12 +00: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) {
|
2022-11-01 10:13:45 +00:00
|
|
|
k, _, ok := strings.Cut(val, "=")
|
|
|
|
if k == "" {
|
2020-08-14 14:54:08 +00:00
|
|
|
return "", errors.New("invalid environment variable: " + val)
|
2016-12-23 19:09:12 +00:00
|
|
|
}
|
2022-11-01 10:13:45 +00:00
|
|
|
if ok {
|
2016-12-23 19:09:12 +00:00
|
|
|
return val, nil
|
|
|
|
}
|
2022-11-01 10:13:45 +00:00
|
|
|
if envVal, ok := os.LookupEnv(k); ok {
|
|
|
|
return k + "=" + envVal, nil
|
2016-12-23 19:09:12 +00:00
|
|
|
}
|
2020-08-14 14:54:08 +00:00
|
|
|
return val, nil
|
2016-12-23 19:09:12 +00:00
|
|
|
}
|