From 23d838c1b6d6c1232335357856e914627a82d76e Mon Sep 17 00:00:00 2001 From: George Suntres Date: Mon, 27 Apr 2026 17:08:57 -0400 Subject: [PATCH] Make GetProjectRoot return arbitrary levels of parent directories. --- fs.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/fs.go b/fs.go index c6e6a1e..3587c4c 100644 --- a/fs.go +++ b/fs.go @@ -15,25 +15,20 @@ func Rootify(path string) string { return filepath.Join(root, path) } -// func GetProjectRoot() string { -// _, b, _, _ := runtime.Caller(0) -// // b is the absolute path to this specific .go file -// // 1st Dir() gets the folder containing the file -// // 2nd Dir() gets the parent of that folder -// return filepath.Dir(filepath.Dir(b)) -// } - -func GetProjectRoot() (string, error) { - exePath, err := os.Executable() +func GetProjectRoot(dir... bool) (string, error) { + exeDir, err := os.Executable() if err != nil { return "", err } - exeDir := filepath.Dir(exePath) + for i, d := range dir { + exeDir = filepath.Dir(exeDir) - // // adjust depending on your structure - // // e.g. binary is in /project/bin/app → go up one level - // root := filepath.Dir(exeDir) + // up to 3 + if i > 3 { + break + } + } return exeDir, nil }