Skip to content

Commit d86a138

Browse files
committed
Improve readability and correctness of all functions and comments
1 parent c36ab3c commit d86a138

2 files changed

Lines changed: 25 additions & 29 deletions

File tree

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Go env
22

3-
Lightweight Go package that helps loading the environment variables from an `.env` file that can be stored anywhere in
4-
the project's structure.
3+
A lightweight Go package that loads environment variables from an .env file, which can be located anywhere within the
4+
project's structure.
55

6-
This package does not contain any additional dependencies.
6+
This package has no external dependencies.
77

88
## Install
99

@@ -31,8 +31,3 @@ func main() {
3131
// Example: addr := os.Getenv("ADDR")
3232
}
3333
```
34-
35-
## License
36-
Copyright (c) 2025 [Raimon Espasa Bou](https://github.com/raiesbo)
37-
38-
Licensed under [MIT License](./LICENSE)

main.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,38 @@ import (
66
"strings"
77
)
88

9-
// getPaths receives an array of directory entries and transforms it into a array of file paths concatenating the
10-
// directory name with a base path.
9+
const (
10+
EnvFile = ".env"
11+
)
12+
13+
// getPaths converts an array of directory entries into an array of file paths by concatenating each
14+
// entry's name with a base path.
1115
func getPaths(newDirs []os.DirEntry, basePath string) []string {
12-
var paths []string
16+
paths := make([]string, len(newDirs))
1317

14-
for _, dir := range newDirs {
15-
path := filepath.Join(basePath, dir.Name())
16-
paths = append(paths, path)
18+
for i, dir := range newDirs {
19+
paths[i] = filepath.Join(basePath, dir.Name())
1720
}
1821

1922
return paths
2023
}
2124

22-
// loadVarsFromFile receives a path to an .env file, parses it and loads all the variables.
25+
// loadVarsFromFile parses an .env file at the given path and loads its variables into the environment.
2326
func loadVarsFromFile(path string) error {
2427
fileData, err := os.ReadFile(path)
2528
if err != nil {
2629
return err
2730
}
2831

2932
for _, line := range strings.Split(string(fileData), "\n") {
30-
lineContent := strings.Split(line, "=")
31-
if len(lineContent) == 2 {
32-
if err := os.Setenv(lineContent[0], lineContent[1]); err != nil {
33+
if line == "" || strings.HasPrefix(line, "#") {
34+
continue
35+
}
36+
37+
parts := strings.Split(line, "=")
38+
if len(parts) == 2 {
39+
key, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
40+
if err := os.Setenv(key, value); err != nil {
3341
return err
3442
}
3543
}
@@ -38,17 +46,10 @@ func loadVarsFromFile(path string) error {
3846
return nil
3947
}
4048

41-
// Load reads recursively all the directories of a project until it finds a .env file. Once the .env is found, reads
42-
// the file and loads the values as OS ENV values.
49+
// Load recursively scans all directories of a project until it finds a .env file. Once found, it reads
50+
// the file and loads its values as environment variables.
4351
func Load() error {
44-
var dirsQueue []string
45-
46-
dirs, err := os.ReadDir("./")
47-
if err != nil {
48-
return err
49-
}
50-
51-
dirsQueue = getPaths(dirs, ".")
52+
dirsQueue := []string{"./"}
5253

5354
for len(dirsQueue) > 0 {
5455
path := dirsQueue[0]
@@ -64,7 +65,7 @@ func Load() error {
6465
return err
6566
}
6667
dirsQueue = append(dirsQueue, getPaths(children, path)...)
67-
} else if file.Name() == ".env" {
68+
} else if file.Name() == EnvFile {
6869
return loadVarsFromFile(path)
6970
}
7071

0 commit comments

Comments
 (0)