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
16 changes: 12 additions & 4 deletions opts/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
"unicode"
Expand All @@ -12,15 +13,22 @@ import (

const whiteSpaces = " \t"

func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([]string, error) {
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) {
lines := []string{}
scanner := bufio.NewScanner(fh)
scanner := bufio.NewScanner(r)
currentLine := 0
utf8bom := []byte{0xEF, 0xBB, 0xBF}
for scanner.Scan() {
Expand Down Expand Up @@ -53,8 +61,8 @@ func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([]
lines = append(lines, variable+"="+value)
} else {
var present bool
if emptyFn != nil {
value, present = emptyFn(line)
if lookupFn != nil {
value, present = lookupFn(line)
}
if present {
// if only a pass-through variable is given, clean it up.
Expand Down
26 changes: 26 additions & 0 deletions opts/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package opts

import (
"bytes"
"testing"

"gotest.tools/v3/assert"
)

func TestParseKeyValueFile(t *testing.T) {
b := []byte(`
FOO=BAR
ZOT`)

vars := map[string]string{
"ZOT": "QIX",
}
lookupFn := func(s string) (string, bool) {
v, ok := vars[s]
return v, ok
}

got, err := ParseKeyValueFile(bytes.NewReader(b), "(inlined)", lookupFn)
assert.NilError(t, err)
assert.DeepEqual(t, got, []string{"FOO=BAR", "ZOT=QIX"})
}