DetectWorkspace walks up the directory tree from cwd looking for .pad.toml.
(flagOverride string)
| 23 | |
| 24 | // DetectWorkspace walks up the directory tree from cwd looking for .pad.toml. |
| 25 | func DetectWorkspace(flagOverride string) (string, error) { |
| 26 | if flagOverride != "" { |
| 27 | return flagOverride, nil |
| 28 | } |
| 29 | |
| 30 | cwd, err := os.Getwd() |
| 31 | if err != nil { |
| 32 | return "", err |
| 33 | } |
| 34 | |
| 35 | dir := cwd |
| 36 | for { |
| 37 | configPath := filepath.Join(dir, ".pad.toml") |
| 38 | if _, err := os.Stat(configPath); err == nil { |
| 39 | var cfg PadToml |
| 40 | if _, err := toml.DecodeFile(configPath, &cfg); err != nil { |
| 41 | return "", fmt.Errorf("parse %s: %w", configPath, err) |
| 42 | } |
| 43 | if cfg.Workspace != "" { |
| 44 | return cfg.Workspace, nil |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | parent := filepath.Dir(dir) |
| 49 | if parent == dir { |
| 50 | break |
| 51 | } |
| 52 | dir = parent |
| 53 | } |
| 54 | |
| 55 | return "", fmt.Errorf("no workspace linked. Run 'pad workspace init' to create one") |
| 56 | } |
| 57 | |
| 58 | // LoadPadToml finds and reads the nearest .pad.toml by walking up from cwd. |
| 59 | // Returns nil if no .pad.toml is found (not an error). |
no test coverage detected