Find is like [Open] except it recursively searches up the directory tree, starting in path. It returns [ErrNotFound] if path is a valid directory and neither it nor any of its parents contain a config file. Find stops searching as soon as it encounters a file with a well-known config name (such as
(path string)
| 123 | // Find stops searching as soon as it encounters a file with a well-known config |
| 124 | // name (such as devbox.json), even if that config fails to load. |
| 125 | func Find(path string) (*Config, error) { |
| 126 | start := time.Now() |
| 127 | slog.Debug("searching for config file (including parent directories)", "path", path) |
| 128 | |
| 129 | cfg, err := open(path) |
| 130 | if errors.Is(err, ErrNotFound) { |
| 131 | cfg, err = searchParentDirs(path) |
| 132 | } |
| 133 | |
| 134 | if err == nil { |
| 135 | slog.Debug("config file found", "path", cfg.Root.AbsRootPath, "dur", time.Since(start)) |
| 136 | } else { |
| 137 | slog.Error("config file search error", "err", err.Error(), "dur", time.Since(start)) |
| 138 | } |
| 139 | return cfg, err |
| 140 | } |
| 141 | |
| 142 | // searchDir looks for a config file in dir. It does not search parent |
| 143 | // directories. |