file.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package mhayaFile
  2. import (
  3. "os"
  4. "path"
  5. "path/filepath"
  6. "runtime"
  7. "sort"
  8. "strings"
  9. cerr "github.com/mhaya/error"
  10. cslice "github.com/mhaya/extend/slice"
  11. )
  12. func JudgeFile(filePath string) (string, bool) {
  13. if filePath == "" {
  14. return filePath, false
  15. }
  16. var p, n string
  17. index := strings.LastIndex(filePath, "/")
  18. if index > 0 {
  19. p = filePath[0:index]
  20. n = filePath[index+1:]
  21. } else {
  22. p = "./"
  23. n = filePath
  24. }
  25. newPath, found := JudgePath(p)
  26. if !found {
  27. return "", false
  28. }
  29. fullFilePath := filepath.Join(newPath, n)
  30. if IsFile(fullFilePath) {
  31. return fullFilePath, true
  32. }
  33. return "", false
  34. }
  35. func JudgePath(filePath string) (string, bool) {
  36. tmpPath := filepath.Join(GetWorkDir(), filePath)
  37. ok := IsDir(tmpPath)
  38. if ok {
  39. return tmpPath, true
  40. }
  41. tmpPath = filepath.Join(GetCurrentDirectory(), filePath)
  42. ok = IsDir(tmpPath)
  43. if ok {
  44. return tmpPath, true
  45. }
  46. dir := GetStackDir()
  47. for _, d := range dir {
  48. tmpPath = filepath.Join(d, filePath)
  49. if IsDir(tmpPath) {
  50. return tmpPath, true
  51. }
  52. }
  53. if IsDir(filePath) {
  54. return filePath, true
  55. }
  56. return "", false
  57. }
  58. func IsDir(dirPath string) bool {
  59. info, err := os.Stat(dirPath)
  60. if err == nil && info.IsDir() {
  61. return true
  62. }
  63. return false
  64. }
  65. func IsFile(fullPath string) bool {
  66. info, err := os.Stat(fullPath)
  67. if err == nil && !info.IsDir() {
  68. return true
  69. }
  70. return false
  71. }
  72. func GetCurrentDirectory() string {
  73. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  74. if err != nil {
  75. panic(err)
  76. }
  77. return strings.Replace(dir, "\\", "/", -1)
  78. }
  79. func GetCurrentPath() string {
  80. var absPath string
  81. _, filename, _, ok := runtime.Caller(0)
  82. if ok {
  83. absPath = path.Dir(filename)
  84. }
  85. return absPath
  86. }
  87. func GetStackDir() []string {
  88. var dir []string
  89. var buf [2 << 16]byte
  90. stack := string(buf[:runtime.Stack(buf[:], true)])
  91. lines := strings.Split(strings.TrimSpace(stack), "\n")
  92. for _, line := range lines {
  93. lastLine := strings.TrimSpace(line)
  94. lastIndex := strings.LastIndex(lastLine, "/")
  95. if lastIndex < 1 {
  96. continue
  97. }
  98. thisDir := lastLine[:lastIndex]
  99. if _, err := os.Stat(thisDir); err != nil {
  100. continue
  101. }
  102. if _, ok := cslice.StringIn(thisDir, dir); ok {
  103. continue
  104. }
  105. dir = append(dir, thisDir)
  106. }
  107. sort.Sort(sort.Reverse(sort.StringSlice(dir)))
  108. return dir
  109. }
  110. func GetWorkDir() string {
  111. p, err := os.Getwd()
  112. if err != nil {
  113. panic(err)
  114. }
  115. return p
  116. }
  117. func JoinPath(elem ...string) (string, error) {
  118. filePath := filepath.Join(elem...)
  119. err := CheckPath(filePath)
  120. if err != nil {
  121. return filePath, err
  122. }
  123. return filePath, nil
  124. }
  125. func CheckPath(filePath string) error {
  126. _, err := os.Stat(filePath)
  127. if err == nil {
  128. return nil
  129. }
  130. return err
  131. }
  132. func GetFileName(filePath string, removeExt bool) string {
  133. fileName := path.Base(filePath)
  134. if !removeExt {
  135. return fileName
  136. }
  137. suffix := path.Ext(fileName)
  138. return strings.TrimSuffix(fileName, suffix)
  139. }
  140. func WalkFiles(rootPath string, fileSuffix string) []string {
  141. var files []string
  142. rootPath, found := JudgePath(rootPath)
  143. if !found {
  144. return files
  145. }
  146. filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
  147. if fileSuffix != "" && !strings.HasSuffix(path, fileSuffix) {
  148. return nil
  149. }
  150. files = append(files, path)
  151. return nil
  152. })
  153. return files
  154. }
  155. func ReadDir(rootPath string, filePrefix, fileSuffix string) ([]string, error) {
  156. var files []string
  157. rootPath, found := JudgePath(rootPath)
  158. if !found {
  159. return files, cerr.Errorf("path = %s, file not found.", rootPath)
  160. }
  161. fileInfo, err := os.ReadDir(rootPath)
  162. if err != nil {
  163. return nil, err
  164. }
  165. for _, info := range fileInfo {
  166. if info.IsDir() {
  167. continue
  168. }
  169. if filePrefix != "" && !strings.HasPrefix(info.Name(), filePrefix) {
  170. continue
  171. }
  172. if fileSuffix != "" && !strings.HasSuffix(info.Name(), fileSuffix) {
  173. continue
  174. }
  175. files = append(files, info.Name())
  176. }
  177. return files, nil
  178. }