123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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()
- 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")
- if err != nil {
- logger.WithFields(logrus.Fields{
- "file": "config.ini",
- }).Panicf("CONFIG FILE IS NOT FOUND (%v)", err)
- }
- // 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)
- }
|