36 lines
580 B
Go
36 lines
580 B
Go
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 ""
|
|
}
|