file.go 3.4 KB

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