1 Star 0 Fork 0

LonelyPale / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dir.go 2.86 KB
一键复制 编辑 原始数据 按行查看 历史
LonelyPale 提交于 2021-07-23 09:37 . update dir Rootify
package goutils
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
)
// 空目录
func EmptyDir(dir string) bool {
dirs, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
return true
}
return len(dirs) == 0
}
// 非空目录
func NonEmptyDir(dir string) bool {
dirs, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
return false
}
return len(dirs) > 0
}
// file and folder
func FileExist(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// file and folder
func FileNotExist(path string) bool {
_, err := os.Stat(path)
return os.IsNotExist(err)
}
// 确保完整路径
func EnsureDir(dir string, mode os.FileMode) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, mode); err != nil {
return fmt.Errorf("could not create directory %v. %v", dir, err)
}
}
return nil
}
// helper function to make dir creation independent of root dir
func Rootify(path string, roots ...string) string {
if filepath.IsAbs(path) {
return path
}
if len(roots) > 0 && len(roots[0]) > 0 {
return filepath.Join(roots[0], path)
}
p, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return p
}
// DefaultDataDir is the default data directory
func DefaultDataDir(dirs ...string) string {
var dir string
if len(dirs) > 0 {
dir = dirs[0]
}
// Try to place the data folder in the user's home dir
home := HomeDir()
if home == "" {
return ""
}
switch runtime.GOOS {
case "windows":
// We used to put everything in %HOME%\AppData\Roaming, but this caused
// problems with non-typical setups. If this fallback location exists and
// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
fallback := filepath.Join(home, "AppData", "Roaming", dir)
appdata := WindowsAppData()
if appdata == "" || NonEmptyDir(fallback) {
return fallback
}
return filepath.Join(appdata, dir)
case "darwin":
return filepath.Join(home, "Library", "Application Support", dir)
default:
return filepath.Join(home, dir)
}
}
func HomeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}
func WindowsAppData() string {
v := os.Getenv("LOCALAPPDATA")
if v == "" {
// Windows XP and below don't have LocalAppData. Crash here because
// we don't support Windows XP and undefining the variable will cause
// other issues.
panic("environment variable LocalAppData is undefined")
}
return v
}
//返回当前可执行文件的路径
//os.Getwd() 返回当前的 work dir 工作目录
func GetCurrentPath() string {
file, err := exec.LookPath(os.Args[0])
if err != nil {
panic(err)
}
path, err := filepath.Abs(file)
if err != nil {
panic(err)
}
index := strings.LastIndex(path, string(os.PathSeparator))
ret := path[:index]
return ret
}
Go
1
https://gitee.com/lonelypalegit/goutils.git
git@gitee.com:lonelypalegit/goutils.git
lonelypalegit
goutils
goutils
master

搜索帮助