Repository navigation
Expand file tree
/
Copy pathaddpath.go
More file actions
113 lines (106 loc) · 2.96 KB
/
Copy pathaddpath.go
File metadata and controls
113 lines (106 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
ignore "github.com/sabhiram/go-gitignore"
)
// getCommentSymbol returns start symbol to comment based on file extension
func getCommentSymbol(path string) string {
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(path), "."))
comment, ok := commentSymbols[ext]
if !ok {
comment = defaultCommentSymbol
}
return comment
}
// commentSymbols describes different comment symbols for different file extensions.
var commentSymbols = map[string]string{
"go": "//",
"py": "#",
"js": "//",
"ts": "//",
"java": "//",
"cpp": "//",
"c": "//",
"cs": "//",
"rb": "#",
"sh": "#",
"php": "//",
"sql": "--",
".mod": "//",
}
const (
defaultCommentSymbol = "#" // is used for files which does not exist in commentSymbols.
dummyCommentTemplate = "%s %s" // simple comment template.
)
// checkCommentTemplate validate format of comment template.
func checkCommentTemplate(template string) error {
if template == "" {
return errors.New("comment template is empty")
}
if strings.Count(template, "%s") != 2 {
return errors.New("comment template must contain two %s")
}
return nil
}
// writePathCommentToFiles writes comment with path to beginning of all files that did not skip by skip.
func writePathCommentToFiles(dir string, ignoreFiles *ignore.GitIgnore, commentTemplate string) error {
if dir == "" {
slog.Info("dir is empty")
return errors.New("dir is empty")
}
if ignoreFiles == nil {
slog.Info("ignore files matcher is missing")
return errors.New("ignore files matcher is missing")
}
if err := checkCommentTemplate(commentTemplate); err != nil {
return err
}
absDir, err := filepath.Abs(dir)
if err != nil {
slog.Error("error getting absolute path", slog.Any("err", err))
return err
}
walker := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if ignoreFiles.MatchesPath(path) {
return nil
}
if info.IsDir() {
return nil
}
relPath, err := filepath.Rel(absDir, path)
if err != nil {
slog.Error("error getting relative path", slog.String("path", path), slog.Any("err", err))
return err
}
content, err := os.ReadFile(path)
if err != nil {
slog.Error("error reading file", slog.String("path", path), slog.Any("err", err))
return err
}
const newLineChar = "\n"
if !strings.HasSuffix(commentTemplate, newLineChar) {
commentTemplate += newLineChar
}
pathComment := fmt.Sprintf(commentTemplate, getCommentSymbol(relPath), relPath)
updatedContent := append([]byte(pathComment), content...)
if err = os.WriteFile(path, updatedContent, info.Mode()); err != nil {
slog.Error("error writing to file", slog.String("path", path), slog.Any("err", err))
return err
}
slog.Debug("add path comment to file", slog.String("file", relPath))
return nil
}
if err = filepath.Walk(absDir, walker); err != nil {
slog.Error("err processing directory")
return err
}
return nil
}