main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "git.ali33.ru/fcg-xvii/go-tools/text/config"
  9. _ "git.ali33.ru/fcg-xvii/go-tools/text/config/ini"
  10. "github.com/sirupsen/logrus"
  11. )
  12. var (
  13. logger = logrus.New()
  14. SourceDir = "."
  15. LogDir = "."
  16. CommentPosition = "top"
  17. CommentLine = 0
  18. CommentString = ""
  19. FileExtensions = []string{
  20. "go",
  21. "py",
  22. }
  23. CommentTemplates = map[string]string{
  24. "go": "// %s",
  25. "py": "# %s",
  26. "js": "// %s",
  27. "html": "<!-- %s -->",
  28. "sh": "# %s",
  29. }
  30. CommentTemplateDefault = "// %s"
  31. )
  32. func GetTemplate(ext string) string {
  33. res := CommentTemplateDefault
  34. if len(ext) > 0 {
  35. if tpl, check := CommentTemplates[ext[1:]]; check {
  36. res = tpl
  37. }
  38. }
  39. return res
  40. }
  41. func GetCommentString(ext string) string {
  42. tpl := GetTemplate(ext)
  43. return fmt.Sprintf(tpl, CommentString)
  44. }
  45. func GetCommentData(ext string) []byte {
  46. return []byte(GetCommentString(ext))
  47. }
  48. func CheckExtension(ext string) bool {
  49. if len(ext) == 0 {
  50. return false
  51. }
  52. ext = ext[1:]
  53. for _, lExt := range FileExtensions {
  54. if lExt == ext {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. func CheckCommentExists(data []byte, ext string) bool {
  61. cData := GetCommentData(ext)
  62. switch CommentPosition {
  63. case "top":
  64. return bytes.Index(data, cData) == 0
  65. case "bottom":
  66. return bytes.Index(data, cData) == len(data)-len(cData)
  67. // todo line !!!
  68. default:
  69. return false
  70. }
  71. }
  72. func CommentAppend(data []byte, ext string) []byte {
  73. cData := GetCommentString(ext)
  74. switch CommentPosition {
  75. case "top":
  76. return append([]byte(cData+"\n"), data...)
  77. case "bottom":
  78. return append(data, []byte("\n"+cData)...)
  79. // todo line !!!
  80. default:
  81. return data
  82. }
  83. }
  84. func ReadDir(path string) {
  85. info, err := os.Stat(path)
  86. if err != nil {
  87. logger.WithFields(logrus.Fields{
  88. "dir_path": path,
  89. "err": err,
  90. }).Warn("Read directory error")
  91. return
  92. }
  93. if !info.IsDir() {
  94. logger.WithFields(logrus.Fields{
  95. "dir_path": path,
  96. "err": "Expected directory",
  97. }).Warn("Read directory error")
  98. return
  99. }
  100. items, err := os.ReadDir(path)
  101. if err != nil {
  102. logger.WithFields(logrus.Fields{
  103. "dir_path": path,
  104. "err": err,
  105. }).Warn("Read directory error")
  106. return
  107. }
  108. for _, item := range items {
  109. itemPath := path + "/" + item.Name()
  110. if item.IsDir() {
  111. ReadDir(itemPath)
  112. } else {
  113. // check extension
  114. ext := filepath.Ext(itemPath)
  115. if !CheckExtension(ext) {
  116. continue
  117. }
  118. data, err := ioutil.ReadFile(itemPath)
  119. if err != nil {
  120. logger.WithFields(logrus.Fields{
  121. "file_path": itemPath,
  122. "err": err,
  123. }).Error("File read error")
  124. }
  125. // check comment already exists
  126. if CheckCommentExists(data, ext) {
  127. logger.WithFields(logrus.Fields{
  128. "file_path": itemPath,
  129. "err": "Comment already exists",
  130. }).Warn("pass")
  131. continue
  132. }
  133. // append comment
  134. f, err := os.OpenFile(itemPath, os.O_WRONLY|os.O_TRUNC, item.Type().Perm())
  135. if err != nil {
  136. logger.WithFields(logrus.Fields{
  137. "file_path": itemPath,
  138. "err": err,
  139. }).Error("File write error")
  140. }
  141. f.Write(CommentAppend(data, ext))
  142. f.Close()
  143. }
  144. }
  145. }
  146. func main() {
  147. // parse config
  148. conf, err := config.FromFile("ini", "config.ini")
  149. if err != nil {
  150. logger.WithFields(logrus.Fields{
  151. "file": "config.ini",
  152. }).Panicf("CONFIG FILE IS NOT FOUND (%v)", err)
  153. }
  154. // setup variables
  155. conf.ValueSetup("source_dir", &SourceDir)
  156. conf.ValueSetup("log_dir", &LogDir)
  157. conf.ValueSetup("comment_position", &CommentPosition)
  158. conf.ValueSetup("comment_line", &CommentLine)
  159. conf.ValueSetup("comment_string", &CommentString)
  160. conf.ValueSetup("file_extensions", &FileExtensions)
  161. // read directory
  162. ReadDir(SourceDir)
  163. }