- SQLite/redka 数据目录相对可执行文件解析,自动创建 data/ 父目录 - ./run.sh build 产物改为 ./aiops(与 run.sh 同级),数据/日志在 ./data ./logs - 解决从项目根执行时 data/ 不存在导致的 SQLite CANTOPEN 报错
36 lines
849 B
Go
36 lines
849 B
Go
// appdir — 独立二进制运行目录(数据/配置相对可执行文件,不依赖 cwd)// Logan.Li
|
|
package appdir
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// Root 可执行文件所在目录(解析软链)// Logan.Li
|
|
func Root() string {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return "."
|
|
}
|
|
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
|
|
exe = resolved
|
|
}
|
|
return filepath.Dir(exe)
|
|
}
|
|
|
|
// Resolve 相对路径 → 基于可执行文件目录的绝对路径 // Logan.Li
|
|
func Resolve(path string) string {
|
|
if path == "" || filepath.IsAbs(path) {
|
|
return path
|
|
}
|
|
return filepath.Join(Root(), path)
|
|
}
|
|
|
|
// EnsureParentDir 确保文件父目录存在 // Logan.Li
|
|
func EnsureParentDir(filePath string) error {
|
|
dir := filepath.Dir(filePath)
|
|
if dir == "" || dir == "." {
|
|
return nil
|
|
}
|
|
return os.MkdirAll(dir, 0o755)
|
|
} |