-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
58 lines (50 loc) · 1.12 KB
/
file.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
54
55
56
57
58
package util
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
// GetCurrentAbPath 最终方案-全兼容
func GetCurrentAbPath() string {
dir := GetCurrentAbPathByExecutable()
if strings.Contains(dir, getTmpDir()) {
return GetCurrentAbPathByCaller(0)
}
return dir
}
// 获取系统临时目录,兼容go run
func getTmpDir() string {
dir := os.Getenv("TEMP")
if dir == "" {
dir = os.Getenv("TMP")
}
res, _ := filepath.EvalSymlinks(dir)
return res
}
// GetCurrentAbPathByExecutable 获取当前执行文件绝对路径
func GetCurrentAbPathByExecutable() string {
exePath, err := os.Executable()
if err != nil {
panic(err)
}
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
return res
}
// GetCurrentAbPathByCaller 获取当前执行文件绝对路径(go run)
func GetCurrentAbPathByCaller(skip int) string {
var abPath string
_, filename, _, ok := runtime.Caller(skip)
if ok {
abPath = path.Dir(filename)
}
return abPath
}
func AbPath(file string) string {
if strings.HasPrefix(file, "/") {
return file
}
return fmt.Sprintf("%s/%s", GetCurrentAbPathByCaller(2), file)
}