summaryrefslogtreecommitdiff
path: root/cmd/kpod/parse.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/kpod/parse.go')
-rw-r--r--cmd/kpod/parse.go63
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.