file.go 5.1 KB

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