1
1
package configfile
2
2
3
3
import (
4
- "bufio"
5
4
"fmt"
6
5
"os"
7
6
"path/filepath"
8
- "strings"
7
+
8
+ "github.com/hashicorp/go-envparse"
9
9
)
10
10
11
11
func (c * ConfigFile ) IsEnvsecEnabled () bool {
@@ -32,32 +32,10 @@ func (c *ConfigFile) ParseEnvsFromDotEnv() (map[string]string, error) {
32
32
}
33
33
defer file .Close ()
34
34
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 )
57
38
}
58
39
59
- if err := scanner .Err (); err != nil {
60
- return nil , fmt .Errorf ("failed to read env file: %v" , err )
61
- }
62
40
return envMap , nil
63
41
}
0 commit comments