file.go 3.2 KB

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