liuhaijun e94826ce29 add server
Change-Id: I0760f17f6a01c0121b59fcbfafc666032dbc30af
2024-09-19 09:44:15 +00:00

91 lines
1.9 KiB
Go
Raw Permalink 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 utils
import (
"errors"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
// If 模拟简单的三元操作
func If(condition bool, trueVal, falseVal any) any {
if condition {
return trueVal
}
return falseVal
}
// GetRunPath 获取执行目录作为默认目录
func GetRunPath() string {
currentPath, err := os.Getwd()
if err != nil {
return ""
}
return currentPath
}
// GetFileDirectoryToCaller 根据运行堆栈信息获取文件目录skip 默认1
func GetFileDirectoryToCaller(opts ...int) (directory string, ok bool) {
var filename string
directory = ""
skip := 1
if opts != nil {
skip = opts[0]
}
if _, filename, _, ok = runtime.Caller(skip); ok {
directory = path.Dir(filename)
}
return
}
// GetCurrentAbPathByExecutable 获取当前执行文件绝对路径
func GetCurrentAbPathByExecutable() (string, error) {
exePath, err := os.Executable()
if err != nil {
return "", err
}
res, _ := filepath.EvalSymlinks(exePath)
return filepath.Dir(res), nil
}
// GetCurrentPath 获取当前执行文件路径,如果是临时目录则获取当前文件的的执行路径
func GetCurrentPath() (dir string, err error) {
dir, err = GetCurrentAbPathByExecutable()
if err != nil {
return "", err
}
tmpDir, err := filepath.EvalSymlinks(os.TempDir())
if err != nil {
return "", err
}
if strings.Contains(dir, tmpDir) {
var ok bool
if dir, ok = GetFileDirectoryToCaller(2); !ok {
return "", errors.New("failed to get path")
}
}
return dir, nil
}
// GetDefaultPath 获取当前执行文件路径,如果是临时目录则获取运行命令的工作目录
func GetDefaultPath() (dir string, err error) {
dir, err = GetCurrentAbPathByExecutable()
if err != nil {
return "", err
}
tmpDir, err := filepath.EvalSymlinks(os.TempDir())
if err != nil {
return "", err
}
if strings.Contains(dir, tmpDir) {
return GetRunPath(), nil
}
return dir, nil
}