Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion opts/envfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package opts

import (
"fmt"
"os"
)

Expand All @@ -18,5 +19,14 @@ import (
// environment variables, that's why we just strip leading whitespace and
// nothing more.
func ParseEnvFile(filename string) ([]string, error) {
return parseKeyValueFile(filename, os.LookupEnv)
fh, err := os.Open(filename)
if err != nil {
return []string{}, err
}
out, err := parseKeyValueFile(fh, os.LookupEnv)
_ = fh.Close()
if err != nil {
return []string{}, fmt.Errorf("invalid env file (%s): %v", filename, err)
}
return out, nil
}
22 changes: 8 additions & 14 deletions opts/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@ import (
"bytes"
"fmt"
"io"
"os"
"strings"
"unicode"
"unicode/utf8"
)

const whiteSpaces = " \t"

func parseKeyValueFile(filename string, lookupFn func(string) (string, bool)) ([]string, error) {
fh, err := os.Open(filename)
if err != nil {
return []string{}, err
}
defer fh.Close()
return ParseKeyValueFile(fh, filename, lookupFn)
}

// ParseKeyValueFile parse a file containing key,value pairs separated by equal sign
// Lines starting with `#` are ignored
// If a key is declared without a value (no equal sign), lookupFn is requested to provide value for the given key
// value is returned as-is, without any kind of parsing but removal of leading whitespace
func ParseKeyValueFile(r io.Reader, filename string, lookupFn func(string) (string, bool)) ([]string, error) {
func ParseKeyValueFile(r io.Reader, lookupFn func(string) (string, bool)) ([]string, error) {
return parseKeyValueFile(r, lookupFn)
}

const whiteSpaces = " \t"

func parseKeyValueFile(r io.Reader, lookupFn func(string) (string, bool)) ([]string, error) {
lines := []string{}
scanner := bufio.NewScanner(r)
currentLine := 0
utf8bom := []byte{0xEF, 0xBB, 0xBF}
for scanner.Scan() {
scannedBytes := scanner.Bytes()
if !utf8.Valid(scannedBytes) {
return []string{}, fmt.Errorf("env file %s contains invalid utf8 bytes at line %d: %v", filename, currentLine+1, scannedBytes)
return []string{}, fmt.Errorf("invalid utf8 bytes at line %d: %v", currentLine+1, scannedBytes)
}
// We trim UTF8 BOM
if currentLine == 0 {
Expand Down
2 changes: 1 addition & 1 deletion opts/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ZOT`)
return v, ok
}

got, err := ParseKeyValueFile(bytes.NewReader(b), "(inlined)", lookupFn)
got, err := ParseKeyValueFile(bytes.NewReader(b), lookupFn)
assert.NilError(t, err)
assert.DeepEqual(t, got, []string{"FOO=BAR", "ZOT=QIX"})
}
10 changes: 8 additions & 2 deletions opts/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opts

import (
"errors"
"fmt"
"os"
"strconv"
"strings"
Expand All @@ -25,9 +26,14 @@ func ReadKVEnvStrings(files []string, override []string) ([]string, error) {
func readKVStrings(files []string, override []string, emptyFn func(string) (string, bool)) ([]string, error) {
var variables []string
for _, ef := range files {
parsedVars, err := parseKeyValueFile(ef, emptyFn)
fh, err := os.Open(ef)
if err != nil {
return nil, err
return []string{}, err
}
parsedVars, err := parseKeyValueFile(fh, emptyFn)
_ = fh.Close()
if err != nil {
return nil, fmt.Errorf("invalid env file (%s): %v", ef, err)
}
variables = append(variables, parsedVars...)
}
Expand Down