cmd: Enhance .env (dotenv) file parsing

Basic support for quoted values, newlines in quoted values, and comments.

Does not support variable or command expansion.
This commit is contained in:
Matthew Holt 2022-04-13 11:35:28 -06:00
parent bc15b4b0e7
commit 30b6d1f47a
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 212 additions and 16 deletions

View file

@ -368,42 +368,68 @@ func loadEnvFromFile(envFile string) error {
return nil return nil
} }
// parseEnvFile parses an env file from KEY=VALUE format.
// It's pretty naive. Limited value quotation is supported,
// but variable and command expansions are not supported.
func parseEnvFile(envInput io.Reader) (map[string]string, error) { func parseEnvFile(envInput io.Reader) (map[string]string, error) {
envMap := make(map[string]string) envMap := make(map[string]string)
scanner := bufio.NewScanner(envInput) scanner := bufio.NewScanner(envInput)
var line string var lineNumber int
lineNumber := 0
for scanner.Scan() { for scanner.Scan() {
line = strings.TrimSpace(scanner.Text()) line := strings.TrimSpace(scanner.Text())
lineNumber++ lineNumber++
// skip lines starting with comment // skip empty lines and lines starting with comment
if strings.HasPrefix(line, "#") { if line == "" || strings.HasPrefix(line, "#") {
continue
}
// skip empty line
if len(line) == 0 {
continue continue
} }
// split line into key and value
fields := strings.SplitN(line, "=", 2) fields := strings.SplitN(line, "=", 2)
if len(fields) != 2 { if len(fields) != 2 {
return nil, fmt.Errorf("can't parse line %d; line should be in KEY=VALUE format", lineNumber) return nil, fmt.Errorf("can't parse line %d; line should be in KEY=VALUE format", lineNumber)
} }
key, val := fields[0], fields[1]
if strings.Contains(fields[0], " ") { // sometimes keys are prefixed by "export " so file can be sourced in bash; ignore it here
return nil, fmt.Errorf("bad key on line %d: contains whitespace", lineNumber) key = strings.TrimPrefix(key, "export ")
}
key := fields[0]
val := fields[1]
// validate key and value
if key == "" { if key == "" {
return nil, fmt.Errorf("missing or empty key on line %d", lineNumber) return nil, fmt.Errorf("missing or empty key on line %d", lineNumber)
} }
if strings.Contains(key, " ") {
return nil, fmt.Errorf("invalid key on line %d: contains whitespace: %s", lineNumber, key)
}
if strings.HasPrefix(val, " ") || strings.HasPrefix(val, "\t") {
return nil, fmt.Errorf("invalid value on line %d: whitespace before value: '%s'", lineNumber, val)
}
// remove any trailing comment after value
if commentStart := strings.Index(val, "#"); commentStart > 0 {
before := val[commentStart-1]
if before == '\t' || before == ' ' {
val = strings.TrimRight(val[:commentStart], " \t")
}
}
// quoted value: support newlines
if strings.HasPrefix(val, `"`) {
for !(strings.HasSuffix(line, `"`) && !strings.HasSuffix(line, `\"`)) {
val = strings.ReplaceAll(val, `\"`, `"`)
if !scanner.Scan() {
break
}
lineNumber++
line = strings.ReplaceAll(scanner.Text(), `\"`, `"`)
val += "\n" + line
}
val = strings.TrimPrefix(val, `"`)
val = strings.TrimSuffix(val, `"`)
}
envMap[key] = val envMap[key] = val
} }

170
cmd/main_test.go Normal file
View file

@ -0,0 +1,170 @@
package caddycmd
import (
"reflect"
"strings"
"testing"
)
func TestParseEnvFile(t *testing.T) {
for i, tc := range []struct {
input string
expect map[string]string
shouldErr bool
}{
{
input: `KEY=value`,
expect: map[string]string{
"KEY": "value",
},
},
{
input: `
KEY=value
OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
"OTHER_KEY": "Some Value",
},
},
{
input: `
KEY=value
INVALID KEY=asdf
OTHER_KEY=Some Value
`,
shouldErr: true,
},
{
input: `
KEY=value
SIMPLE_QUOTED="quoted value"
OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
"SIMPLE_QUOTED": "quoted value",
"OTHER_KEY": "Some Value",
},
},
{
input: `
KEY=value
NEWLINES="foo
bar"
OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
"NEWLINES": "foo\n\tbar",
"OTHER_KEY": "Some Value",
},
},
{
input: `
KEY=value
ESCAPED="\"escaped quotes\"
here"
OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
"ESCAPED": "\"escaped quotes\"\nhere",
"OTHER_KEY": "Some Value",
},
},
{
input: `
export KEY=value
OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
"OTHER_KEY": "Some Value",
},
},
{
input: `
=value
OTHER_KEY=Some Value
`,
shouldErr: true,
},
{
input: `
EMPTY=
OTHER_KEY=Some Value
`,
expect: map[string]string{
"EMPTY": "",
"OTHER_KEY": "Some Value",
},
},
{
input: `
EMPTY=""
OTHER_KEY=Some Value
`,
expect: map[string]string{
"EMPTY": "",
"OTHER_KEY": "Some Value",
},
},
{
input: `
KEY=value
#OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
},
},
{
input: `
KEY=value
COMMENT=foo bar # some comment here
OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
"COMMENT": "foo bar",
"OTHER_KEY": "Some Value",
},
},
{
input: `
KEY=value
WHITESPACE= foo
OTHER_KEY=Some Value
`,
shouldErr: true,
},
{
input: `
KEY=value
WHITESPACE=" foo bar "
OTHER_KEY=Some Value
`,
expect: map[string]string{
"KEY": "value",
"WHITESPACE": " foo bar ",
"OTHER_KEY": "Some Value",
},
},
} {
actual, err := parseEnvFile(strings.NewReader(tc.input))
if err != nil && !tc.shouldErr {
t.Errorf("Test %d: Got error but shouldn't have: %v", i, err)
}
if err == nil && tc.shouldErr {
t.Errorf("Test %d: Did not get error but should have", i)
}
if tc.shouldErr {
continue
}
if !reflect.DeepEqual(tc.expect, actual) {
t.Errorf("Test %d: Expected %v but got %v", i, tc.expect, actual)
}
}
}