env-file: update functionality and docs

Multiple flags allowed. Order prescribed. Examples provided. Multiline
accounted for.

Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)
This commit is contained in:
Vincent Batts 2014-03-06 17:49:47 -05:00
parent 321381301e
commit e659708bcc

View file

@ -2,9 +2,10 @@ package opts
import ( import (
"bufio" "bufio"
"bytes" "fmt"
"io"
"os" "os"
"strconv"
"strings"
) )
/* /*
@ -17,30 +18,20 @@ func ParseEnvFile(filename string) ([]string, error) {
} }
defer fh.Close() defer fh.Close()
var ( lines := []string{}
lines []string = []string{} scanner := bufio.NewScanner(fh)
line, chunk []byte for scanner.Scan() {
) line := scanner.Text()
reader := bufio.NewReader(fh) // line is not empty, and not starting with '#'
line, isPrefix, err := reader.ReadLine() if len(line) > 0 && !strings.HasPrefix(line, "#") && strings.Contains(line, "=") {
data := strings.SplitN(line, "=", 2)
for err == nil { key := data[0]
if isPrefix { val := data[1]
chunk = append(chunk, line...) if str, err := strconv.Unquote(data[1]); err == nil {
} else if !isPrefix && len(chunk) > 0 { val = str
line = chunk }
chunk = []byte{} lines = append(lines, fmt.Sprintf("%s=%s", key, val))
} 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 return lines, nil
} }