Skip to content

Commit

Permalink
Used go-envparse to parse .env files (#2177)
Browse files Browse the repository at this point in the history
## Summary
TSIA

Will hold off on updating the docs until the `flakepath` discussion is
resolved.

## How was it tested?
devbox run test
  • Loading branch information
mohsenari committed Jun 28, 2024
1 parent 1252033 commit 94de0d6
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 deletions internal/devconfig/configfile/env.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package configfile

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/go-envparse"
)

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

envMap := map[string]string{}

// Read the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Ideally .env file shouldn't have empty lines and comments but
// this check makes it allowed.
if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid line in .env file: %s", line)
}
// Also ideally, .env files should not have space in their `key=value` format
// but this allows `key = value` to pass through as well
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])

// Add the parsed key-value pair to the map
envMap[key] = value
envMap, err := envparse.Parse(file)
if err != nil {
return nil, fmt.Errorf("failed to parse env file: %v", err)
}

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

0 comments on commit 94de0d6

Please sign in to comment.