Find returns the first .env file CAM should load. When custom is non-empty and exists, it is returned. When custom is set but missing and strict is true, ErrNotFound is returned. When custom is empty, Find walks the current working directory upward until it locates a .env file or hits a directory
(custom string, strict bool)
| 28 | // dotenv.find_dotenv behaviour), then falls back to ~/.env and |
| 29 | // ~/.config/code-agent-manager/.env. |
| 30 | func Find(custom string, strict bool) (string, error) { |
| 31 | if custom != "" { |
| 32 | expanded := pathutil.Expand(custom) |
| 33 | if pathutil.Exists(expanded) { |
| 34 | return expanded, nil |
| 35 | } |
| 36 | if strict { |
| 37 | return "", ErrNotFound |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if dir, err := os.Getwd(); err == nil { |
| 42 | if found := walkUpward(dir); found != "" { |
| 43 | return found, nil |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | for _, candidate := range []string{ |
| 48 | filepath.Join(pathutil.Home(), ".env"), |
| 49 | filepath.Join(pathutil.ConfigDir(), ".env"), |
| 50 | } { |
| 51 | if pathutil.Exists(candidate) { |
| 52 | return candidate, nil |
| 53 | } |
| 54 | } |
| 55 | return "", ErrNotFound |
| 56 | } |
| 57 | |
| 58 | func walkUpward(start string) string { |
| 59 | dir := start |