directory.go 977 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "os"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "go.uber.org/zap"
  6. )
  7. //@author: [piexlmax](https://github.com/piexlmax)
  8. //@function: PathExists
  9. //@description: 文件目录是否存在
  10. //@param: path string
  11. //@return: bool, error
  12. func PathExists(path string) (bool, error) {
  13. _, err := os.Stat(path)
  14. if err == nil {
  15. return true, nil
  16. }
  17. if os.IsNotExist(err) {
  18. return false, nil
  19. }
  20. return false, err
  21. }
  22. //@author: [piexlmax](https://github.com/piexlmax)
  23. //@function: CreateDir
  24. //@description: 批量创建文件夹
  25. //@param: dirs ...string
  26. //@return: err error
  27. func CreateDir(dirs ...string) (err error) {
  28. for _, v := range dirs {
  29. exist, err := PathExists(v)
  30. if err != nil {
  31. return err
  32. }
  33. if !exist {
  34. global.GVA_LOG.Debug("create directory" + v)
  35. if err := os.MkdirAll(v, os.ModePerm); err != nil {
  36. global.GVA_LOG.Error("create directory"+v, zap.Any(" error:", err))
  37. return err
  38. }
  39. }
  40. }
  41. return err
  42. }