package utils
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"errors"
|
|
)
|
|
|
|
func GetCurrentDir() string {
|
|
file, err := exec.LookPath(os.Args[0])
|
|
if err != nil {
|
|
c, _ := os.Getwd()
|
|
return c
|
|
}
|
|
return strings.Replace(filepath.Dir(file), "\\", "/", -1)
|
|
}
|
|
|
|
func GetAbsolutePath(path string) string {
|
|
p := GetAbsolutePathNoFilter(path)
|
|
p = strings.Replace(p, "\\", "/", -1) // 统一使用/
|
|
for {
|
|
a := strings.Index(p, "//")
|
|
if a == -1 {
|
|
break
|
|
}
|
|
p = strings.Replace(p, "//", "/", -1)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func GetAbsolutePathNoFilter(path string) string {
|
|
if strings.Index(path, "file:/") == 0 {
|
|
path = path[5:]
|
|
mpos := strings.Index(path, ":")
|
|
if mpos >= 0 {
|
|
//去除开头斜杠, 如: ///C:/test
|
|
for {
|
|
if len(path) > 0 && (path[0] == '/' || path[0] == '\\') {
|
|
path = path[1:]
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
return (path)
|
|
}
|
|
|
|
for {
|
|
if len(path) > 1 && (path[0] == '/' || path[0] == '\\') && (path[1] == '/' || path[1] == '\\') {
|
|
path = path[1:]
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
path, _ = filepath.Abs(path)
|
|
return (path)
|
|
} else if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
|
|
r, _ := filepath.Abs(GetCurrentDir() + path[1:])
|
|
return (r)
|
|
}
|
|
r, _ := filepath.Abs(path)
|
|
return (r)
|
|
}
|
|
|
|
func GetDir(path string) string {
|
|
if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
|
|
return filepath.ToSlash(filepath.Dir(GetCurrentDir() + path[1:]))
|
|
}
|
|
return filepath.ToSlash(filepath.Dir(path))
|
|
}
|
|
|
|
func EnsureDir(dir string) string {
|
|
fullDir := GetAbsolutePath(dir)
|
|
if IsExists(fullDir) {
|
|
return fullDir
|
|
}
|
|
|
|
os.MkdirAll(fullDir, 777)
|
|
return fullDir
|
|
}
|
|
|
|
func EnsureFilePath(filePath string) string {
|
|
filePath = GetAbsolutePath(filePath)
|
|
EnsureDir(GetDir(filePath))
|
|
return filePath
|
|
}
|
|
|
|
func IsExists(path string) bool {
|
|
if path == "" {
|
|
return false
|
|
}
|
|
_, err := os.Stat(path)
|
|
if err != nil && os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
|
|
/******************************************************************************
|
|
*
|
|
* @Function Name : GetCurrentPath
|
|
*-----------------------------------------------------------------------------
|
|
*
|
|
* @Description : windows下获取传入相对路径的绝对路径
|
|
*
|
|
* @Function Parameters : dir 相对路径
|
|
*
|
|
* @Return Value : string 绝对路径
|
|
*
|
|
* @Return Value : error 执行出现的错误
|
|
*
|
|
* @Author : zhangxin
|
|
*
|
|
* @Date : 2021-03-19
|
|
*
|
|
******************************************************************************/
|
|
func GetCurrentPath(dir string) (string, error) {
|
|
file, err := exec.LookPath(os.Args[0])
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
path, err := filepath.Abs(file)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
i := strings.LastIndex(path, "/")
|
|
if i < 0 {
|
|
i = strings.LastIndex(path, "\\")
|
|
}
|
|
if i < 0 {
|
|
return "", errors.New(`error: Can't find "/" or "\".`)
|
|
}
|
|
pathdir := path[0 : i+1]
|
|
if len(dir) > 0 {
|
|
dir = strings.Replace(dir, "/", "\\", -1)
|
|
return filepath.Join(pathdir, dir), nil
|
|
}
|
|
return path[0 : i+1], nil
|
|
}
|