GAAS 广汽安道拓GFrame金属件MOM项目
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.
 

97 lines
1.9 KiB

package utils
import (
"os"
"os/exec"
"path/filepath"
"strings"
)
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
}