file.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. goctlHome, err := GetGoctlHome()
  80. if err != nil {
  81. return "", err
  82. }
  83. return filepath.Join(goctlHome, version.GetGoctlVersion(), category), nil
  84. }
  85. // InitTemplates creates template files GoctlHome where could get it by GetGoctlHome
  86. func InitTemplates(category string, templates map[string]string) error {
  87. dir, err := GetTemplateDir(category)
  88. if err != nil {
  89. return err
  90. }
  91. if err := MkdirIfNotExist(dir); err != nil {
  92. return err
  93. }
  94. for k, v := range templates {
  95. if err := createTemplate(filepath.Join(dir, k), v, false); err != nil {
  96. return err
  97. }
  98. }
  99. return nil
  100. }
  101. // CreateTemplate writes template into file even it is exists
  102. func CreateTemplate(category, name, content string) error {
  103. dir, err := GetTemplateDir(category)
  104. if err != nil {
  105. return err
  106. }
  107. return createTemplate(filepath.Join(dir, name), content, true)
  108. }
  109. // Clean deletes all templates and removes the parent directory
  110. func Clean(category string) error {
  111. dir, err := GetTemplateDir(category)
  112. if err != nil {
  113. return err
  114. }
  115. return os.RemoveAll(dir)
  116. }
  117. // LoadTemplate gets template content by the specified file
  118. func LoadTemplate(category, file, builtin string) (string, error) {
  119. dir, err := GetTemplateDir(category)
  120. if err != nil {
  121. return "", err
  122. }
  123. file = filepath.Join(dir, file)
  124. if !FileExists(file) {
  125. return builtin, nil
  126. }
  127. content, err := ioutil.ReadFile(file)
  128. if err != nil {
  129. return "", err
  130. }
  131. return string(content), nil
  132. }
  133. // SameFile compares the between path if the same path,
  134. // it maybe the same path in case case-ignore, such as:
  135. // /Users/go_zero and /Users/Go_zero, as far as we know,
  136. // this case maybe appear on macOS and Windows.
  137. func SameFile(path1, path2 string) (bool, error) {
  138. stat1, err := os.Stat(path1)
  139. if err != nil {
  140. return false, err
  141. }
  142. stat2, err := os.Stat(path2)
  143. if err != nil {
  144. return false, err
  145. }
  146. return os.SameFile(stat1, stat2), nil
  147. }
  148. func createTemplate(file, content string, force bool) error {
  149. if FileExists(file) && !force {
  150. return nil
  151. }
  152. f, err := os.Create(file)
  153. if err != nil {
  154. return err
  155. }
  156. defer f.Close()
  157. _, err = f.WriteString(content)
  158. return err
  159. }
  160. // MustTempDir creates a temporary directory
  161. func MustTempDir() string {
  162. dir, err := ioutil.TempDir("", "")
  163. if err != nil {
  164. log.Fatalln(err)
  165. }
  166. return dir
  167. }