Project root of binary not file
This commit is contained in:
39
fs.go
39
fs.go
@@ -1,18 +1,41 @@
|
||||
package sys
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func Rootify(path string) string {
|
||||
return filepath.Join(GetProjectRoot(), path)
|
||||
root, err := GetProjectRoot()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
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))
|
||||
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()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
exeDir := filepath.Dir(exePath)
|
||||
|
||||
// adjust depending on your structure
|
||||
// e.g. binary is in /project/bin/app → go up one level
|
||||
root := filepath.Dir(exeDir)
|
||||
|
||||
return root, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user