forked from stulzq/azure-openai-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
53 lines (48 loc) · 1.27 KB
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package util
import (
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
func GetCurrentAbsPath() (string, bool) {
// Work in dev and prod runtime
isDebug := false
absDir := getCurrentAbsPathByExecutable()
tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
// If it is a temporary directory or the directory contains 'GoLand' keyword,
// then retrieve it using the `caller` method.
if strings.Contains(absDir, tmpDir) || strings.Contains(absDir, "GoLand") {
isDebug = true
return getCurrentAbsPathByCaller(), isDebug
}
return absDir, isDebug
}
func GetWorkdir() string {
workDir, isDebug := GetCurrentAbsPath()
if isDebug {
workDir = filepath.Join(workDir, "..")
}
return workDir
}
// getCurrentAbsPathByExecutable Retrieve the absolute path to the currently executing file.
func getCurrentAbsPathByExecutable() string {
exePath, err := os.Executable()
log.Printf("executable path: %s", exePath)
if err != nil {
panic(err)
}
res, _ := filepath.EvalSymlinks(exePath)
return path.Dir(res)
}
// getCurrentAbsPathByCaller Retrieve the absolute path to the currently executing file.(by `go run`)
func getCurrentAbsPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(filename)
}
return abPath
}