file.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package pathx
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "github.com/logrusorgru/aurora"
  12. "github.com/zeromicro/go-zero/tools/goctl/internal/version"
  13. )
  14. // NL defines a new line.
  15. const (
  16. NL = "\n"
  17. goctlDir = ".goctl"
  18. gitDir = ".git"
  19. autoCompleteDir = ".auto_complete"
  20. cacheDir = "cache"
  21. )
  22. var goctlHome string
  23. // RegisterGoctlHome register goctl home path.
  24. func RegisterGoctlHome(home string) {
  25. goctlHome = home
  26. }
  27. // CreateIfNotExist creates a file if it is not exists.
  28. func CreateIfNotExist(file string) (*os.File, error) {
  29. _, err := os.Stat(file)
  30. if !os.IsNotExist(err) {
  31. return nil, fmt.Errorf("%s already exist", file)
  32. }
  33. return os.Create(file)
  34. }
  35. // RemoveIfExist deletes the specified file if it is exists.
  36. func RemoveIfExist(filename string) error {
  37. if !FileExists(filename) {
  38. return nil
  39. }
  40. return os.Remove(filename)
  41. }
  42. // RemoveOrQuit deletes the specified file if read a permit command from stdin.
  43. func RemoveOrQuit(filename string) error {
  44. if !FileExists(filename) {
  45. return nil
  46. }
  47. fmt.Printf("%s exists, overwrite it?\nEnter to overwrite or Ctrl-C to cancel...",
  48. aurora.BgRed(aurora.Bold(filename)))
  49. bufio.NewReader(os.Stdin).ReadBytes('\n')
  50. return os.Remove(filename)
  51. }
  52. // FileExists returns true if the specified file is exists.
  53. func FileExists(file string) bool {
  54. _, err := os.Stat(file)
  55. return err == nil
  56. }
  57. // FileNameWithoutExt returns a file name without suffix.
  58. func FileNameWithoutExt(file string) string {
  59. return strings.TrimSuffix(file, filepath.Ext(file))
  60. }
  61. // GetGoctlHome returns the path value of the goctl, the default path is ~/.goctl, if the path has
  62. // been set by calling the RegisterGoctlHome method, the user-defined path refers to.
  63. func GetGoctlHome() (string, error) {
  64. if len(goctlHome) != 0 {
  65. return goctlHome, nil
  66. }
  67. return GetDefaultGoctlHome()
  68. }
  69. // GetDefaultGoctlHome returns the path value of the goctl home where Join $HOME with .goctl.
  70. func GetDefaultGoctlHome() (string, error) {
  71. home, err := os.UserHomeDir()
  72. if err != nil {
  73. return "", err
  74. }
  75. return filepath.Join(home, goctlDir), nil
  76. }
  77. // GetGitHome returns the git home of goctl.
  78. func GetGitHome() (string, error) {
  79. goctlH, err := GetGoctlHome()
  80. if err != nil {
  81. return "", err
  82. }
  83. return filepath.Join(goctlH, gitDir), nil
  84. }
  85. // GetAutoCompleteHome returns the auto_complete home of goctl.
  86. func GetAutoCompleteHome() (string, error) {
  87. goctlH, err := GetGoctlHome()
  88. if err != nil {
  89. return "", err
  90. }
  91. return filepath.Join(goctlH, autoCompleteDir), nil
  92. }
  93. // GetCacheDir returns the cache dit of goctl.
  94. func GetCacheDir() (string, error) {
  95. goctlH, err := GetGoctlHome()
  96. if err != nil {
  97. return "", err
  98. }
  99. return filepath.Join(goctlH, cacheDir), nil
  100. }
  101. // GetTemplateDir returns the category path value in GoctlHome where could get it by GetGoctlHome.
  102. func GetTemplateDir(category string) (string, error) {
  103. home, err := GetGoctlHome()
  104. if err != nil {
  105. return "", err
  106. }
  107. if home == goctlHome {
  108. // backward compatible, it will be removed in the feature
  109. // backward compatible start.
  110. beforeTemplateDir := filepath.Join(home, version.GetGoctlVersion(), category)
  111. fs, _ := ioutil.ReadDir(beforeTemplateDir)
  112. var hasContent bool
  113. for _, e := range fs {
  114. if e.Size() > 0 {
  115. hasContent = true
  116. }
  117. }
  118. if hasContent {
  119. return beforeTemplateDir, nil
  120. }
  121. // backward compatible end.
  122. return filepath.Join(home, category), nil
  123. }
  124. return filepath.Join(home, version.GetGoctlVersion(), category), nil
  125. }
  126. // InitTemplates creates template files GoctlHome where could get it by GetGoctlHome.
  127. func InitTemplates(category string, templates map[string]string) error {
  128. dir, err := GetTemplateDir(category)
  129. if err != nil {
  130. return err
  131. }
  132. if err := MkdirIfNotExist(dir); err != nil {
  133. return err
  134. }
  135. for k, v := range templates {
  136. if err := createTemplate(filepath.Join(dir, k), v, false); err != nil {
  137. return err
  138. }
  139. }
  140. return nil
  141. }
  142. // CreateTemplate writes template into file even it is exists.
  143. func CreateTemplate(category, name, content string) error {
  144. dir, err := GetTemplateDir(category)
  145. if err != nil {
  146. return err
  147. }
  148. return createTemplate(filepath.Join(dir, name), content, true)
  149. }
  150. // Clean deletes all templates and removes the parent directory.
  151. func Clean(category string) error {
  152. dir, err := GetTemplateDir(category)
  153. if err != nil {
  154. return err
  155. }
  156. return os.RemoveAll(dir)
  157. }
  158. // LoadTemplate gets template content by the specified file.
  159. func LoadTemplate(category, file, builtin string) (string, error) {
  160. dir, err := GetTemplateDir(category)
  161. if err != nil {
  162. return "", err
  163. }
  164. file = filepath.Join(dir, file)
  165. if !FileExists(file) {
  166. return builtin, nil
  167. }
  168. content, err := ioutil.ReadFile(file)
  169. if err != nil {
  170. return "", err
  171. }
  172. return string(content), nil
  173. }
  174. // SameFile compares the between path if the same path,
  175. // it maybe the same path in case case-ignore, such as:
  176. // /Users/go_zero and /Users/Go_zero, as far as we know,
  177. // this case maybe appear on macOS and Windows.
  178. func SameFile(path1, path2 string) (bool, error) {
  179. stat1, err := os.Stat(path1)
  180. if err != nil {
  181. return false, err
  182. }
  183. stat2, err := os.Stat(path2)
  184. if err != nil {
  185. return false, err
  186. }
  187. return os.SameFile(stat1, stat2), nil
  188. }
  189. func createTemplate(file, content string, force bool) error {
  190. if FileExists(file) && !force {
  191. return nil
  192. }
  193. f, err := os.Create(file)
  194. if err != nil {
  195. return err
  196. }
  197. defer f.Close()
  198. _, err = f.WriteString(content)
  199. return err
  200. }
  201. // MustTempDir creates a temporary directory.
  202. func MustTempDir() string {
  203. dir, err := ioutil.TempDir("", "")
  204. if err != nil {
  205. log.Fatalln(err)
  206. }
  207. return dir
  208. }
  209. func Copy(src, dest string) error {
  210. f, err := os.Open(src)
  211. if err != nil {
  212. return err
  213. }
  214. defer f.Close()
  215. dir := filepath.Dir(dest)
  216. err = MkdirIfNotExist(dir)
  217. if err != nil {
  218. return err
  219. }
  220. w, err := os.Create(dest)
  221. if err != nil {
  222. return err
  223. }
  224. w.Chmod(os.ModePerm)
  225. defer w.Close()
  226. _, err = io.Copy(w, f)
  227. return err
  228. }