godocdb/utils/utils.go

36 lines
580 B
Go
Raw Normal View History

2025-06-08 10:31:34 +08:00
package utils
import (
"os"
"path/filepath"
)
func GetRootDir() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
rootDir := filepath.VolumeName(dir)
if rootDir == "" {
return "/"
}
return rootDir
}
func GetProjectRootDir() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
rootDir := GetRootDir()
for dir != rootDir {
//check go.mod file under dir
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
dir = filepath.Dir(dir)
}
return ""
}