2016-12-23 14:09:12 -05:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-08-14 10:38:09 -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:38:09 -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:38:09 -04:00
|
|
|
// are not validated, and it's up to the application inside the container
|
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:38:09 -04:00
|
|
|
arr := strings.SplitN(val, "=", 2)
|
2016-12-23 14:09:12 -05:00
|
|
|
if arr[0] == "" {
|
2020-08-14 10:38:09 -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:38:09 -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:38:09 -04:00
|
|
|
return val, nil
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|