diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2017-11-07 10:03:46 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-11-20 16:25:31 +0000 |
commit | 57599f0075ccab859d4158f7ee891b9b971c731f (patch) | |
tree | 84bb48fc3ef1321a29816710cbb1221cc598b745 /cmd/kpod/parse.go | |
parent | 3b72af614777b966671ad0eb0c5dbde0eeedcfa2 (diff) | |
download | podman-57599f0075ccab859d4158f7ee891b9b971c731f.tar.gz podman-57599f0075ccab859d4158f7ee891b9b971c731f.tar.bz2 podman-57599f0075ccab859d4158f7ee891b9b971c731f.zip |
Fix up handling of environment variables
The way docker works is if a user specifies a non `-e Name=Value`, IE
just a `-e Name`, then the environment variable Name from the clients
OS.ENV is used.
Also by default Docker containers run with the HOSTNAME environment set
to the HOSTNAME specified for the container.
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
Closes: #21
Approved by: baude
Diffstat (limited to 'cmd/kpod/parse.go')
-rw-r--r-- | cmd/kpod/parse.go | 63 |
1 files changed, 35 insertions, 28 deletions
diff --git a/cmd/kpod/parse.go b/cmd/kpod/parse.go index e3143a793..7f6fc78df 100644 --- a/cmd/kpod/parse.go +++ b/cmd/kpod/parse.go @@ -327,55 +327,62 @@ func doesEnvExist(name string) bool { // reads a file of line terminated key=value pairs, and overrides any keys // present in the file with additional pairs specified in the override parameter // for env-file and labels-file flags -func readKVStrings(files []string, override []string) ([]string, error) { - envVariables := []string{} +func readKVStrings(env map[string]string, files []string, override []string) error { for _, ef := range files { - parsedVars, err := parseEnvFile(ef) - if err != nil { - return nil, err + if err := parseEnvFile(env, ef); err != nil { + return err + } + } + for _, line := range override { + if err := parseEnv(env, line); err != nil { + return err } - envVariables = append(envVariables, parsedVars...) } - // parse the '-e' and '--env' after, to allow override - envVariables = append(envVariables, override...) + return nil +} + +func parseEnv(env map[string]string, line string) error { + data := strings.SplitN(line, "=", 2) + + // trim the front of a variable, but nothing else + name := strings.TrimLeft(data[0], whiteSpaces) + if strings.ContainsAny(name, whiteSpaces) { + return errors.Errorf("name %q has white spaces, poorly formatted name", name) + } - return envVariables, nil + if len(data) > 1 { + env[name] = data[1] + } else { + // if only a pass-through variable is given, clean it up. + val, exists := os.LookupEnv(name) + if !exists { + return errors.Errorf("environment variable %q does not exist", name) + } + env[name] = val + } + return nil } // parseEnvFile reads a file with environment variables enumerated by lines -func parseEnvFile(filename string) ([]string, error) { +func parseEnvFile(env map[string]string, filename string) error { fh, err := os.Open(filename) if err != nil { - return []string{}, err + return err } defer fh.Close() - lines := []string{} scanner := bufio.NewScanner(fh) for scanner.Scan() { // trim the line from all leading whitespace first line := strings.TrimLeft(scanner.Text(), whiteSpaces) // line is not empty, and not starting with '#' if len(line) > 0 && !strings.HasPrefix(line, "#") { - data := strings.SplitN(line, "=", 2) - - // trim the front of a variable, but nothing else - variable := strings.TrimLeft(data[0], whiteSpaces) - if strings.ContainsAny(variable, whiteSpaces) { - return []string{}, errors.Errorf("variable %q has white spaces, poorly formatted environment", variable) - } - - if len(data) > 1 { - - // pass the value through, no trimming - lines = append(lines, fmt.Sprintf("%s=%s", variable, data[1])) - } else { - // if only a pass-through variable is given, clean it up. - lines = append(lines, fmt.Sprintf("%s=%s", strings.TrimSpace(line), os.Getenv(line))) + if err := parseEnv(env, line); err != nil { + return err } } } - return lines, scanner.Err() + return scanner.Err() } // NsIpc represents the container ipc stack. |