Browse Source

in progress

0x4a52466c696e74 2 years ago
parent
commit
509aed5a3d
4 changed files with 177 additions and 8 deletions
  1. 12 1
      config.ini
  2. 4 4
      go.mod
  3. 6 0
      go.sum
  4. 155 3
      main.go

+ 12 - 1
config.ini

@@ -1 +1,12 @@
-one = one, two, three
+# dir of source code
+source_dir = /home/icewind/projects_new/admefine-release
+# log_dir
+log_dir = .
+# comment_position [ top, bottom, line ]
+comment_position = top
+# comment line (if comment_position = line)
+comment_line = 0
+# comment string
+comment_string = — Admify 2.0. Правообладатель — ООО УК «Нектарин», ОГРН 1087746039886, ИНН 7710703730.
+# file extensions
+file_extensions = go, py, js, html

+ 4 - 4
go.mod

@@ -2,9 +2,9 @@ module commenter
 
 go 1.18
 
-require git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220528093543-d34303d75ee9
-
 require (
-	github.com/sirupsen/logrus v1.8.1 // indirect
-	golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
+	git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220707153406-b7b62781753e
+	github.com/sirupsen/logrus v1.8.1
 )
+
+require golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect

+ 6 - 0
go.sum

@@ -1,5 +1,9 @@
 git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220528093543-d34303d75ee9 h1:ZLYCTYIgXcY6GTDmVfYsfbRNiTki9Ucfu5qCOYYpV+I=
 git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220528093543-d34303d75ee9/go.mod h1:8XpQShSOR7fAiCJg56M2mhf1KxiqnTeGttW/CUuyyCk=
+git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220707140553-1bad2fb7b9bd h1:o+3lGO5N4L0Jf/2t8qhh6sbUa3blw0faOmT5M+GHUYQ=
+git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220707140553-1bad2fb7b9bd/go.mod h1:8XpQShSOR7fAiCJg56M2mhf1KxiqnTeGttW/CUuyyCk=
+git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220707153406-b7b62781753e h1:Aj+pp01MN/uBF8s3ObZ/h8IsF3p611uYu6jHzjMGJEU=
+git.ali33.ru/fcg-xvii/go-tools v0.0.0-20220707153406-b7b62781753e/go.mod h1:8XpQShSOR7fAiCJg56M2mhf1KxiqnTeGttW/CUuyyCk=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
@@ -7,3 +11,5 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
 golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
+golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

+ 155 - 3
main.go

@@ -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)
 }