-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenv.go
47 lines (39 loc) · 1.08 KB
/
dotenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package dotenv
import (
"fmt"
"os"
"path/filepath"
"github.com/joho/godotenv"
)
// Load locates and loads the .env file based on the provided options.
func Load(options ...Option) error {
config := NewConfig(options)
envFile, err := findEnvFile(config)
if err != nil {
return fmt.Errorf("failed to retrieve environment file: %w", err)
}
err = godotenv.Load(envFile)
if err != nil {
return fmt.Errorf("failed to load environment file: %w", err)
}
return nil
}
// findEnvFile returns the full path of the .env file it finds based on the anchor file.
func findEnvFile(config *Config) (string, error) {
currentDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get current directory: %w", err)
}
for {
anchorFilePath := filepath.Join(currentDir, config.AnchorFile)
if _, err := os.Stat(anchorFilePath); err == nil {
break
}
parent := filepath.Dir(currentDir)
if parent == currentDir {
return "", fmt.Errorf("anchor file %s not found", config.AnchorFile)
}
currentDir = parent
}
return filepath.Join(currentDir, config.EnvFilename), nil
}