file.go 4.0 KB

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