Files
azure-openai-proxy/util/path.go
2023-06-15 15:57:37 +08:00

47 lines
1013 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package util
import (
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
func GetCurrentAbsPath() string {
// 最终方案-全兼容
dir := getCurrentAbPathByExecutable()
tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
// 如果是临时目录或者是goland运行的目录那么就用caller的方式获取
if strings.Contains(dir, tmpDir) || strings.Contains(dir, "GoLand") {
return getCurrentAbPathByCaller()
}
return dir
}
func GetWorkdir() string {
currentAbsPath := GetCurrentAbsPath()
workDir := filepath.Join(currentAbsPath, "..")
return workDir
}
// 获取当前执行文件绝对路径
func getCurrentAbPathByExecutable() string {
exePath, err := os.Executable()
if err != nil {
panic(err)
}
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
return res
}
// 获取当前执行文件绝对路径go run
func getCurrentAbPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(filename)
}
return abPath
}