pkg/opts/envfile.go
Vincent Batts e659708bcc 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)
2014-03-31 14:45:13 -04:00

37 lines
766 B
Go

package opts
import (
"bufio"
"fmt"
"os"
"strconv"
"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, "#") && 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))
}
}
return lines, nil
}