This commit is contained in:
parent
84ae0f6994
commit
1d36961a7c
16 changed files with 734 additions and 48 deletions
|
|
@ -3,20 +3,54 @@ package repo
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Root finds the repository root (directory that contains apps/pkg/go.mod
|
||||
// and is not itself under apps/ — i.e. the real checkout root).
|
||||
// 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 drm.exe (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 {
|
||||
wd = "."
|
||||
return "."
|
||||
}
|
||||
cur := wd
|
||||
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++ {
|
||||
// Require apps/pkg/go.mod AND a sibling marker so that walking from
|
||||
// inside apps/pkg does not treat apps/ as the root (apps/pkg/go.mod
|
||||
// would match the old top-level pkg/ layout check).
|
||||
pkgMod := filepath.Join(cur, "apps", "pkg", "go.mod")
|
||||
if _, err := os.Stat(pkgMod); err == nil {
|
||||
if markerOK(cur) {
|
||||
|
|
@ -29,7 +63,7 @@ func Root() string {
|
|||
}
|
||||
cur = parent
|
||||
}
|
||||
return wd
|
||||
return ""
|
||||
}
|
||||
|
||||
func markerOK(root string) bool {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue