support for docker run environment variables file

Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)
This commit is contained in:
Vincent Batts 2014-02-16 19:24:22 -05:00 committed by Vincent Batts
parent 74349e02c6
commit 2ca6939ac2

44
opts/envfile.go Normal file
View file

@ -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
}