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 (
"bufio"
"bytes"
"io"
"fmt"
"os"
"strconv"
"strings"
)
/*
@ -17,30 +18,20 @@ func ParseEnvFile(filename string) ([]string, error) {
}
defer fh.Close()
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{}
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, "#") && strings.Contains(line, "=") {
data := strings.SplitN(line, "=", 2)
key := data[0]
val := data[1]
if str, err := strconv.Unquote(data[1]); err == nil {
val = str
}
lines = append(lines, fmt.Sprintf("%s=%s", key, val))
}
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
}