summaryrefslogtreecommitdiff
path: root/cmd/main.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2022-04-13 11:35:28 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2022-04-13 11:38:20 -0600
commit30b6d1f47a35d00fca7cff0daa2ff59a98c5a85e (patch)
tree61adc9f026e4432d30a732d1f437b4a0234c2345 /cmd/main.go
parentbc15b4b0e799c52be8be86e85e9624141319474d (diff)
cmd: Enhance .env (dotenv) file parsing
Basic support for quoted values, newlines in quoted values, and comments. Does not support variable or command expansion.
Diffstat (limited to 'cmd/main.go')
-rw-r--r--cmd/main.go58
1 files changed, 42 insertions, 16 deletions
diff --git a/cmd/main.go b/cmd/main.go
index f111ba4..498a8ae 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -368,42 +368,68 @@ func loadEnvFromFile(envFile string) error {
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) {
envMap := make(map[string]string)
scanner := bufio.NewScanner(envInput)
- var line string
- lineNumber := 0
+ var lineNumber int
for scanner.Scan() {
- line = strings.TrimSpace(scanner.Text())
+ line := strings.TrimSpace(scanner.Text())
lineNumber++
- // skip lines starting with comment
- if strings.HasPrefix(line, "#") {
- continue
- }
-
- // skip empty line
- if len(line) == 0 {
+ // skip empty lines and lines starting with comment
+ if line == "" || strings.HasPrefix(line, "#") {
continue
}
+ // split line into key and value
fields := strings.SplitN(line, "=", 2)
if len(fields) != 2 {
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], " ") {
- return nil, fmt.Errorf("bad key on line %d: contains whitespace", lineNumber)
- }
-
- key := fields[0]
- val := fields[1]
+ // sometimes keys are prefixed by "export " so file can be sourced in bash; ignore it here
+ key = strings.TrimPrefix(key, "export ")
+ // validate key and value
if key == "" {
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
}