Skip to content

Commit 94de0d6

Browse files
authored
Used go-envparse to parse .env files (#2177)
## Summary TSIA Will hold off on updating the docs until the `flakepath` discussion is resolved. ## How was it tested? devbox run test
1 parent 1252033 commit 94de0d6

File tree

1 file changed

+5
-27
lines changed
  • internal/devconfig/configfile

1 file changed

+5
-27
lines changed
Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package configfile
22

33
import (
4-
"bufio"
54
"fmt"
65
"os"
76
"path/filepath"
8-
"strings"
7+
8+
"github.com/hashicorp/go-envparse"
99
)
1010

1111
func (c *ConfigFile) IsEnvsecEnabled() bool {
@@ -32,32 +32,10 @@ func (c *ConfigFile) ParseEnvsFromDotEnv() (map[string]string, error) {
3232
}
3333
defer file.Close()
3434

35-
envMap := map[string]string{}
36-
37-
// Read the file line by line
38-
scanner := bufio.NewScanner(file)
39-
for scanner.Scan() {
40-
line := scanner.Text()
41-
// Ideally .env file shouldn't have empty lines and comments but
42-
// this check makes it allowed.
43-
if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") {
44-
continue
45-
}
46-
parts := strings.SplitN(line, "=", 2)
47-
if len(parts) != 2 {
48-
return nil, fmt.Errorf("invalid line in .env file: %s", line)
49-
}
50-
// Also ideally, .env files should not have space in their `key=value` format
51-
// but this allows `key = value` to pass through as well
52-
key := strings.TrimSpace(parts[0])
53-
value := strings.TrimSpace(parts[1])
54-
55-
// Add the parsed key-value pair to the map
56-
envMap[key] = value
35+
envMap, err := envparse.Parse(file)
36+
if err != nil {
37+
return nil, fmt.Errorf("failed to parse env file: %v", err)
5738
}
5839

59-
if err := scanner.Err(); err != nil {
60-
return nil, fmt.Errorf("failed to read env file: %v", err)
61-
}
6240
return envMap, nil
6341
}

0 commit comments

Comments
 (0)