From 2ca6939ac27c872f57a3e1985379a88ee6e9311d Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Sun, 16 Feb 2014 19:24:22 -0500 Subject: [PATCH] support for `docker run` environment variables file Docker-DCO-1.1-Signed-off-by: Vincent Batts (github: vbatts) --- opts/envfile.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 opts/envfile.go diff --git a/opts/envfile.go b/opts/envfile.go new file mode 100644 index 0000000..004c320 --- /dev/null +++ b/opts/envfile.go @@ -0,0 +1,44 @@ +package opts + +import ( + "bufio" + "bytes" + "io" + "os" +) + +/* +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 + } + var ( + lines []string = []string{} + line, chunk []byte + ) + reader := bufio.NewReader(fh) + line, isPrefix, err := reader.ReadLine() + + for err == nil { + if isPrefix { + chunk = append(chunk, line...) + } else if !isPrefix && len(chunk) > 0 { + line = chunk + chunk = []byte{} + } else { + chunk = []byte{} + } + + if !isPrefix && len(line) > 0 && bytes.Contains(line, []byte("=")) { + lines = append(lines, string(line)) + } + line, isPrefix, err = reader.ReadLine() + } + if err != nil && err != io.EOF { + return []string{}, err + } + return lines, nil +}