Make GetProjectRoot return arbitrary levels of parent directories.
This commit is contained in:
23
fs.go
23
fs.go
@@ -15,25 +15,20 @@ func Rootify(path string) string {
|
|||||||
return filepath.Join(root, path)
|
return filepath.Join(root, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func GetProjectRoot() string {
|
func GetProjectRoot(dir... bool) (string, error) {
|
||||||
// _, b, _, _ := runtime.Caller(0)
|
exeDir, err := os.Executable()
|
||||||
// // 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()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
exeDir := filepath.Dir(exePath)
|
for i, d := range dir {
|
||||||
|
exeDir = filepath.Dir(exeDir)
|
||||||
|
|
||||||
// // adjust depending on your structure
|
// up to 3
|
||||||
// // e.g. binary is in /project/bin/app → go up one level
|
if i > 3 {
|
||||||
// root := filepath.Dir(exeDir)
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return exeDir, nil
|
return exeDir, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user