|
@@ -1,15 +1,159 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
+ "fmt"
|
|
|
+ "io/ioutil"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+
|
|
|
"git.ali33.ru/fcg-xvii/go-tools/text/config"
|
|
|
_ "git.ali33.ru/fcg-xvii/go-tools/text/config/ini"
|
|
|
"github.com/sirupsen/logrus"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- logger = logrus.New()
|
|
|
+ logger = logrus.New()
|
|
|
+ SourceDir = "."
|
|
|
+ LogDir = "."
|
|
|
+ CommentPosition = "top"
|
|
|
+ CommentLine = 0
|
|
|
+ CommentString = ""
|
|
|
+ FileExtensions = []string{
|
|
|
+ "go",
|
|
|
+ "py",
|
|
|
+ }
|
|
|
+ CommentTemplates = map[string]string{
|
|
|
+ "go": "// %s",
|
|
|
+ "py": "# %s",
|
|
|
+ "js": "// %s",
|
|
|
+ "html": "<!-- %s -->",
|
|
|
+ "sh": "# %s",
|
|
|
+ }
|
|
|
+ CommentTemplateDefault = "// %s"
|
|
|
)
|
|
|
|
|
|
+func GetTemplate(ext string) string {
|
|
|
+ res := CommentTemplateDefault
|
|
|
+ if len(ext) > 0 {
|
|
|
+ if tpl, check := CommentTemplates[ext[1:]]; check {
|
|
|
+ res = tpl
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func GetCommentString(ext string) string {
|
|
|
+ tpl := GetTemplate(ext)
|
|
|
+ return fmt.Sprintf(tpl, CommentString)
|
|
|
+}
|
|
|
+
|
|
|
+func GetCommentData(ext string) []byte {
|
|
|
+ return []byte(GetCommentString(ext))
|
|
|
+}
|
|
|
+
|
|
|
+func CheckExtension(ext string) bool {
|
|
|
+ if len(ext) == 0 {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ ext = ext[1:]
|
|
|
+ for _, lExt := range FileExtensions {
|
|
|
+ if lExt == ext {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+func CheckCommentExists(data []byte, ext string) bool {
|
|
|
+ cData := GetCommentData(ext)
|
|
|
+ switch CommentPosition {
|
|
|
+ case "top":
|
|
|
+ return bytes.Index(data, cData) == 0
|
|
|
+ case "bottom":
|
|
|
+ return bytes.Index(data, cData) == len(data)-len(cData)
|
|
|
+ // todo line !!!
|
|
|
+ default:
|
|
|
+ return false
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func CommentAppend(data []byte, ext string) []byte {
|
|
|
+ cData := GetCommentString(ext)
|
|
|
+ switch CommentPosition {
|
|
|
+ case "top":
|
|
|
+ return append([]byte(cData+"\n"), data...)
|
|
|
+ case "bottom":
|
|
|
+ return append(data, []byte("\n"+cData)...)
|
|
|
+ // todo line !!!
|
|
|
+ default:
|
|
|
+ return data
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func ReadDir(path string) {
|
|
|
+ info, err := os.Stat(path)
|
|
|
+ if err != nil {
|
|
|
+ logger.WithFields(logrus.Fields{
|
|
|
+ "dir_path": path,
|
|
|
+ "err": err,
|
|
|
+ }).Warn("Read directory error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !info.IsDir() {
|
|
|
+ logger.WithFields(logrus.Fields{
|
|
|
+ "dir_path": path,
|
|
|
+ "err": "Expected directory",
|
|
|
+ }).Warn("Read directory error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ items, err := os.ReadDir(path)
|
|
|
+ if err != nil {
|
|
|
+ logger.WithFields(logrus.Fields{
|
|
|
+ "dir_path": path,
|
|
|
+ "err": err,
|
|
|
+ }).Warn("Read directory error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, item := range items {
|
|
|
+ itemPath := path + "/" + item.Name()
|
|
|
+ if item.IsDir() {
|
|
|
+ ReadDir(itemPath)
|
|
|
+ } else {
|
|
|
+ // check extension
|
|
|
+ ext := filepath.Ext(itemPath)
|
|
|
+ if !CheckExtension(ext) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ data, err := ioutil.ReadFile(itemPath)
|
|
|
+ if err != nil {
|
|
|
+ logger.WithFields(logrus.Fields{
|
|
|
+ "file_path": itemPath,
|
|
|
+ "err": err,
|
|
|
+ }).Error("File read error")
|
|
|
+ }
|
|
|
+ // check comment already exists
|
|
|
+ if CheckCommentExists(data, ext) {
|
|
|
+ logger.WithFields(logrus.Fields{
|
|
|
+ "file_path": itemPath,
|
|
|
+ "err": "Comment already exists",
|
|
|
+ }).Warn("pass")
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // append comment
|
|
|
+ f, err := os.OpenFile(itemPath, os.O_WRONLY|os.O_TRUNC, item.Type().Perm())
|
|
|
+ if err != nil {
|
|
|
+ logger.WithFields(logrus.Fields{
|
|
|
+ "file_path": itemPath,
|
|
|
+ "err": err,
|
|
|
+ }).Error("File write error")
|
|
|
+ }
|
|
|
+ f.Write(CommentAppend(data, ext))
|
|
|
+ f.Close()
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
// parse config
|
|
|
conf, err := config.FromFile("ini", "config.ini")
|
|
@@ -18,6 +162,14 @@ func main() {
|
|
|
"file": "config.ini",
|
|
|
}).Panicf("CONFIG FILE IS NOT FOUND (%v)", err)
|
|
|
}
|
|
|
- one := conf.ValueDefault("one", []string{})
|
|
|
- logger.Info(one)
|
|
|
+ // setup variables
|
|
|
+ conf.ValueSetup("source_dir", &SourceDir)
|
|
|
+ conf.ValueSetup("log_dir", &LogDir)
|
|
|
+ conf.ValueSetup("comment_position", &CommentPosition)
|
|
|
+ conf.ValueSetup("comment_line", &CommentLine)
|
|
|
+ conf.ValueSetup("comment_string", &CommentString)
|
|
|
+ conf.ValueSetup("file_extensions", &FileExtensions)
|
|
|
+
|
|
|
+ // read directory
|
|
|
+ ReadDir(SourceDir)
|
|
|
}
|