github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/opts/env.go (about)

     1  package opts
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // ValidateEnv validates an environment variable and returns it.
    11  // If no value is specified, it obtains its value from the current environment
    12  //
    13  // As on ParseEnvFile and related to #16585, environment variable names
    14  // are not validated, and it's up to the application inside the container
    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.SplitN(val, "=", 2)
    20  	if arr[0] == "" {
    21  		return "", errors.New("invalid environment variable: " + val)
    22  	}
    23  	if len(arr) > 1 {
    24  		return val, nil
    25  	}
    26  	if envVal, ok := os.LookupEnv(arr[0]); ok {
    27  		return arr[0] + "=" + envVal, nil
    28  	}
    29  	return val, nil
    30  }