Skip to content

Commit 53fe50c

Browse files
committed
🧱 fix GetWorkdir method in prod mode
1 parent 78b43bd commit 53fe50c

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

util/path.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,49 @@
11
package util
22

33
import (
4+
"log"
45
"os"
56
"path"
67
"path/filepath"
78
"runtime"
89
"strings"
910
)
1011

11-
func GetCurrentAbsPath() string {
12-
// 最终方案-全兼容
13-
dir := getCurrentAbPathByExecutable()
12+
func GetCurrentAbsPath() (string, bool) {
13+
// Work in dev and prod runtime
14+
isDebug := false
15+
absDir := getCurrentAbsPathByExecutable()
1416
tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
15-
// 如果是临时目录或者是goland运行的目录,那么就用caller的方式获取
16-
if strings.Contains(dir, tmpDir) || strings.Contains(dir, "GoLand") {
17-
return getCurrentAbPathByCaller()
17+
// If it is a temporary directory or the directory contains 'GoLand' keyword,
18+
// then retrieve it using the `caller` method.
19+
if strings.Contains(absDir, tmpDir) || strings.Contains(absDir, "GoLand") {
20+
isDebug = true
21+
return getCurrentAbsPathByCaller(), isDebug
1822
}
19-
return dir
23+
return absDir, isDebug
2024
}
2125

2226
func GetWorkdir() string {
23-
currentAbsPath := GetCurrentAbsPath()
24-
workDir := filepath.Join(currentAbsPath, "..")
27+
workDir, isDebug := GetCurrentAbsPath()
28+
if isDebug {
29+
workDir = filepath.Join(workDir, "..")
30+
}
2531
return workDir
2632
}
2733

28-
// 获取当前执行文件绝对路径
29-
func getCurrentAbPathByExecutable() string {
34+
// getCurrentAbsPathByExecutable Retrieve the absolute path to the currently executing file.
35+
func getCurrentAbsPathByExecutable() string {
3036
exePath, err := os.Executable()
37+
log.Printf("executable path: %s", exePath)
3138
if err != nil {
3239
panic(err)
3340
}
34-
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
35-
return res
41+
res, _ := filepath.EvalSymlinks(exePath)
42+
return path.Dir(res)
3643
}
3744

38-
// 获取当前执行文件绝对路径(go run)
39-
func getCurrentAbPathByCaller() string {
45+
// getCurrentAbsPathByCaller Retrieve the absolute path to the currently executing file.(by `go run`
46+
func getCurrentAbsPathByCaller() string {
4047
var abPath string
4148
_, filename, _, ok := runtime.Caller(0)
4249
if ok {

0 commit comments

Comments
 (0)