From 176347382af4dd78ceeb9476dd3b92439f6876cb Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 11 Mar 2014 16:22:58 -0400 Subject: [PATCH] --env-file: simple line-delimited match dock functionality, and not try to achieve shell-sourcing compatibility Docker-DCO-1.1-Signed-off-by: Vincent Batts (github: vbatts) --- opts/envfile.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 opts/envfile.go diff --git a/opts/envfile.go b/opts/envfile.go new file mode 100644 index 0000000000..99a713e761 --- /dev/null +++ b/opts/envfile.go @@ -0,0 +1,35 @@ +package opts + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +/* +Read in a line delimited file with environment variables enumerated +*/ +func ParseEnvFile(filename string) ([]string, error) { + fh, err := os.Open(filename) + if err != nil { + return []string{}, err + } + defer fh.Close() + + lines := []string{} + scanner := bufio.NewScanner(fh) + for scanner.Scan() { + line := scanner.Text() + // line is not empty, and not starting with '#' + if len(line) > 0 && !strings.HasPrefix(line, "#") { + if strings.Contains(line, "=") { + data := strings.SplitN(line, "=", 2) + lines = append(lines, fmt.Sprintf("%s=%s", data[0], data[1])) + } else { + lines = append(lines, fmt.Sprintf("%s=%s", line, os.Getenv(line))) + } + } + } + return lines, nil +}