file.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package util
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/logrusorgru/aurora"
  10. )
  11. // NL defines a new line
  12. const (
  13. NL = "\n"
  14. goctlDir = ".goctl"
  15. )
  16. var goctlHome string
  17. func RegisterGoctlHome(home string) {
  18. goctlHome = home
  19. }
  20. // CreateIfNotExist creates a file if it is not exists
  21. func CreateIfNotExist(file string) (*os.File, error) {
  22. _, err := os.Stat(file)
  23. if !os.IsNotExist(err) {
  24. return nil, fmt.Errorf("%s already exist", file)
  25. }
  26. return os.Create(file)
  27. }
  28. // RemoveIfExist deletes the specified file if it is exists
  29. func RemoveIfExist(filename string) error {
  30. if !FileExists(filename) {
  31. return nil
  32. }
  33. return os.Remove(filename)
  34. }
  35. // RemoveOrQuit deletes the specified file if read a permit command from stdin
  36. func RemoveOrQuit(filename string) error {
  37. if !FileExists(filename) {
  38. return nil
  39. }
  40. fmt.Printf("%s exists, overwrite it?\nEnter to overwrite or Ctrl-C to cancel...",
  41. aurora.BgRed(aurora.Bold(filename)))
  42. bufio.NewReader(os.Stdin).ReadBytes('\n')
  43. return os.Remove(filename)
  44. }
  45. // FileExists returns true if the specified file is exists
  46. func FileExists(file string) bool {
  47. _, err := os.Stat(file)
  48. return err == nil
  49. }
  50. // FileNameWithoutExt returns a file name without suffix
  51. func FileNameWithoutExt(file string) string {
  52. return strings.TrimSuffix(file, filepath.Ext(file))
  53. }
  54. // GetGoctlHome returns the path value of the goctl home where Join $HOME with .goctl
  55. func GetGoctlHome() (string, error) {
  56. if len(goctlHome) != 0 {
  57. return goctlHome, nil
  58. }
  59. home, err := os.UserHomeDir()
  60. if err != nil {
  61. return "", err
  62. }
  63. return filepath.Join(home, goctlDir), nil
  64. }
  65. // GetTemplateDir returns the category path value in GoctlHome where could get it by GetGoctlHome
  66. func GetTemplateDir(category string) (string, error) {
  67. goctlHome, err := GetGoctlHome()
  68. if err != nil {
  69. return "", err
  70. }
  71. return filepath.Join(goctlHome, category), nil
  72. }
  73. // InitTemplates creates template files GoctlHome where could get it by GetGoctlHome
  74. func InitTemplates(category string, templates map[string]string) error {
  75. dir, err := GetTemplateDir(category)
  76. if err != nil {
  77. return err
  78. }
  79. if err := MkdirIfNotExist(dir); err != nil {
  80. return err
  81. }
  82. for k, v := range templates {
  83. if err := createTemplate(filepath.Join(dir, k), v, false); err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. // CreateTemplate writes template into file even it is exists
  90. func CreateTemplate(category, name, content string) error {
  91. dir, err := GetTemplateDir(category)
  92. if err != nil {
  93. return err
  94. }
  95. return createTemplate(filepath.Join(dir, name), content, true)
  96. }
  97. // Clean deletes all templates and removes the parent directory
  98. func Clean(category string) error {
  99. dir, err := GetTemplateDir(category)
  100. if err != nil {
  101. return err
  102. }
  103. return os.RemoveAll(dir)
  104. }
  105. // LoadTemplate gets template content by the specified file
  106. func LoadTemplate(category, file, builtin string) (string, error) {
  107. dir, err := GetTemplateDir(category)
  108. if err != nil {
  109. return "", err
  110. }
  111. file = filepath.Join(dir, file)
  112. if !FileExists(file) {
  113. return builtin, nil
  114. }
  115. content, err := ioutil.ReadFile(file)
  116. if err != nil {
  117. return "", err
  118. }
  119. return string(content), nil
  120. }
  121. func createTemplate(file, content string, force bool) error {
  122. if FileExists(file) && !force {
  123. return nil
  124. }
  125. f, err := os.Create(file)
  126. if err != nil {
  127. return err
  128. }
  129. defer f.Close()
  130. _, err = f.WriteString(content)
  131. return err
  132. }