file.go 4.1 KB

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