You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tool/helper/utils.go

49 lines
992 B

1 year ago
package helper
import (
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
1 year ago
// Ternary 三目运算符
func Ternary(condition bool, trueVal, falseVal interface{}) interface{} {
if condition {
return trueVal
}
return falseVal
}
// GetCurrentAbPath 最终方案-全兼容
func GetCurrentAbPath() string {
dir := GetCurrentAbPathByExecutable()
tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
if strings.Contains(dir, tmpDir) {
return GetCurrentAbPathByCaller()
}
return dir
}
// GetCurrentAbPathByExecutable 获取当前执行文件绝对路径
func GetCurrentAbPathByExecutable() string {
exePath, err := os.Executable()
if err != nil {
log.Fatal(err)
}
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
return res
}
// GetCurrentAbPathByCaller 获取当前执行文件绝对路径go run
func GetCurrentAbPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(filename)
}
return abPath
}