package repo import ( "os" "path/filepath" "strings" ) // Root finds the install / repository root (directory that contains // apps/pkg/go.mod plus a sibling marker such as README.md or outputs/). // // Search order: // 1. Walk up from the current working directory // 2. Walk up from streamd (if the exe lives in bin/, start at the parent) func Root() string { if root := findRoot(wd()); root != "" { return root } if root := findRoot(exeInstallDir()); root != "" { return root } return wd() } func wd() string { wd, err := os.Getwd() if err != nil { return "." } return wd } func exeInstallDir() string { exe, err := os.Executable() if err != nil { return "" } if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved } dir := filepath.Dir(exe) if strings.EqualFold(filepath.Base(dir), "bin") { return filepath.Dir(dir) } return dir } func findRoot(start string) string { if start == "" { return "" } cur := start for i := 0; i < 16; i++ { pkgMod := filepath.Join(cur, "apps", "pkg", "go.mod") if _, err := os.Stat(pkgMod); err == nil { if markerOK(cur) { return cur } } parent := filepath.Dir(cur) if parent == cur { break } cur = parent } return "" } func markerOK(root string) bool { for _, name := range []string{"build.ps1", "README.md", "configs", "outputs"} { if _, err := os.Stat(filepath.Join(root, name)); err == nil { return true } } return false }