125 lines
2.5 KiB
Go
125 lines
2.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"go.uber.org/zap"
|
|
"miniapp/global"
|
|
)
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: PathExists
|
|
//@description: 文件目录是否存在
|
|
//@param: path string
|
|
//@return: bool, error
|
|
|
|
func PathExists(path string) (bool, error) {
|
|
fi, err := os.Stat(path)
|
|
if err == nil {
|
|
if fi.IsDir() {
|
|
return true, nil
|
|
}
|
|
return false, errors.New("存在同名文件")
|
|
}
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: CreateDir
|
|
//@description: 批量创建文件夹
|
|
//@param: dirs ...string
|
|
//@return: err error
|
|
|
|
func CreateDir(dirs ...string) (err error) {
|
|
for _, v := range dirs {
|
|
exist, err := PathExists(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !exist {
|
|
global.GVA_LOG.Debug("create directory" + v)
|
|
if err := os.MkdirAll(v, os.ModePerm); err != nil {
|
|
global.GVA_LOG.Error("create directory"+v, zap.Any(" error:", err))
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
//@author: [songzhibin97](https://github.com/songzhibin97)
|
|
//@function: FileMove
|
|
//@description: 文件移动供外部调用
|
|
//@param: src string, dst string(src: 源位置,绝对路径or相对路径, dst: 目标位置,绝对路径or相对路径,必须为文件夹)
|
|
//@return: err error
|
|
|
|
func FileMove(src string, dst string) (err error) {
|
|
if dst == "" {
|
|
return nil
|
|
}
|
|
src, err = filepath.Abs(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dst, err = filepath.Abs(dst)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
revoke := false
|
|
dir := filepath.Dir(dst)
|
|
Redirect:
|
|
_, err = os.Stat(dir)
|
|
if err != nil {
|
|
err = os.MkdirAll(dir, 0o755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !revoke {
|
|
revoke = true
|
|
goto Redirect
|
|
}
|
|
}
|
|
return os.Rename(src, dst)
|
|
}
|
|
|
|
func DeLFile(filePath string) error {
|
|
return os.RemoveAll(filePath)
|
|
}
|
|
|
|
//@author: [songzhibin97](https://github.com/songzhibin97)
|
|
//@function: TrimSpace
|
|
//@description: 去除结构体空格
|
|
//@param: target interface (target: 目标结构体,传入必须是指针类型)
|
|
//@return: null
|
|
|
|
func TrimSpace(target interface{}) {
|
|
t := reflect.TypeOf(target)
|
|
if t.Kind() != reflect.Ptr {
|
|
return
|
|
}
|
|
t = t.Elem()
|
|
v := reflect.ValueOf(target).Elem()
|
|
for i := 0; i < t.NumField(); i++ {
|
|
switch v.Field(i).Kind() {
|
|
case reflect.String:
|
|
v.Field(i).SetString(strings.TrimSpace(v.Field(i).String()))
|
|
}
|
|
}
|
|
}
|
|
|
|
// FileExist 判断文件是否存在
|
|
func FileExist(path string) bool {
|
|
fi, err := os.Lstat(path)
|
|
if err == nil {
|
|
return !fi.IsDir()
|
|
}
|
|
return !os.IsNotExist(err)
|
|
}
|