file.go 4.8 KB

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