-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
375 lines (321 loc) · 9.05 KB
/
main.go
File metadata and controls
375 lines (321 loc) · 9.05 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// main is main and linters are linters ¯\_(ツ)_/¯
package main
import (
"bytes"
"fmt"
"io"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
const (
readmeFilePath = "./README.md"
problemsTitle = "## Problems"
)
func main() {
fmt.Printf("⛳️ SOLUTIONS ... \n")
NormalizeNames(false)
fmt.Printf("👌 formated \n")
fmt.Printf("💬 COMMENTS ... \n")
NormalizeComments(false)
fmt.Printf("👌 normalized \n")
fmt.Printf("📚 README ... \n")
UpdateReadMe()
fmt.Printf("👌 updated \n")
fmt.Printf("✅ NEW SOLUTIONS ... \n")
AddToGit()
fmt.Printf("👌 git added \n")
}
func AddToGit() {
cmd := "git add ./solutions"
out, err := execCommand(cmd)
if err != nil {
log.Fatalf("git add failed: %s, output: %s", err, out)
}
cmd = "git add ./README.md"
out, err = execCommand(cmd)
if err != nil {
log.Fatalf("git add README failed: %s, output: %s", err, out)
}
// get change list
cmd = "git status --porcelain"
out, err = execCommand(cmd)
if err != nil {
log.Fatalf("git status failed: %s, output: %s", err, out)
}
if out == "" {
fmt.Println("No changes to commit.")
return
}
changedFiles := strings.Split(out, "\n")
cmd = fmt.Sprintf(
"git commit -m \"Update solutions and README: %s\"\n",
strings.Join(changedFiles, ", "),
)
out, err = execCommand(cmd)
if err != nil {
log.Fatalf("git commit failed: %s, output: %s", err, out)
}
cmd = "git push"
out, err = execCommand(cmd)
if err != nil {
log.Fatalf("git push failed: %s, output: %s", err, out)
}
}
func execCommand(cmd string) (string, error) {
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
if err != nil {
return "", fmt.Errorf("command failed: %s, output: %s", err, out)
}
return strings.TrimSpace(string(out)), nil
}
// NormalizeNames renames solutions files to match the pattern s0001_problem_name.go
func NormalizeNames(dry bool) {
err := filepath.WalkDir("./solutions", func(path string, d fs.DirEntry, _ error) error {
skip := map[string]bool{
"main_test.go": true,
"types.go": true,
"utils.go": true,
}
if !d.IsDir() && !skip[d.Name()] && !matchSolutionName(d.Name()) {
problemName := d.Name()
problemName = strings.ToLower(problemName)
problemName = strings.ReplaceAll(problemName, " ", "_")
problemName = strings.Replace(problemName, ".", "", 1)
xs := strings.Split(problemName, "_")
for len(xs[0]) < 4 {
xs[0] = "0" + xs[0]
}
newName := "s" + xs[0] + "_" + strings.Join(xs[1:], "_")
newPath := "./solutions/" + newName
if path != newPath {
fmt.Printf(" %-60s -> %s\n", path, newPath)
if !dry {
renameFile(path, newPath)
}
}
}
return nil
})
if err != nil {
log.Fatalf("format failed: %s", err)
}
}
func renameFile(oldPath, newPath string) {
err := os.Rename(oldPath, newPath)
if err != nil {
log.Fatalf("can't rename file: %s", err)
}
}
func matchSolutionName(name string) bool {
pattern := `^s\d{4}_[\w()]+(\s*-\s*\w+)*\.go$`
re := regexp.MustCompile(pattern)
return re.MatchString(name)
}
// UpdateReadMe syncs solutions list in repo README.md file
func UpdateReadMe() {
file, err := os.OpenFile(readmeFilePath, os.O_RDWR, 0o600)
if err != nil {
log.Fatalf("can't open the file: %s", err)
}
offset, _ := getOffsetOf(file, []byte(problemsTitle))
solutions, _ := getSolutionsList()
data := strings.Join(solutions[1:], "\n")
_, err = file.WriteAt([]byte("\n\n"+data), offset+int64(len(problemsTitle)))
if err != nil {
log.Panicf("can't write file: %s", err)
}
err = file.Close()
if err != nil {
log.Fatalf("can't close the file: %s", err)
}
}
func isValid(path string) bool {
invalidTokens := []string{
"test.go",
"utils.go",
"types.go",
}
for _, t := range invalidTokens {
if strings.Contains(path, t) {
return false
}
}
return true
}
func getSolutionsList() (names []string, err error) {
err = filepath.WalkDir("./solutions", func(path string, _ fs.DirEntry, _ error) error {
if isValid(path) {
words := strings.Split(path, "/")
problemName := strings.Split(words[1], ".")[0]
problemName = "* [" + strings.ReplaceAll(problemName, "_", " ") + "](" + path + ")"
names = append(names, problemName)
}
return nil
})
return names, err
}
func getOffsetOf(f io.ReadSeeker, search []byte) (int64, error) {
data, err := io.ReadAll(f)
if err != nil {
return 0, err
}
return int64(bytes.Index(data, search)), nil
}
// NormalizeComments formats comments in solution files to follow proper formatting rules:
// - lines should not exceed 80 characters
// - word breaks only at whitespace or other break characters
// - entire comment block should be indented at least one tab from left edge
func NormalizeComments(dry bool) {
err := filepath.WalkDir("./solutions", func(path string, d fs.DirEntry, _ error) error {
skip := map[string]bool{
"main_test.go": true,
"types.go": true,
"utils.go": true,
}
if !d.IsDir() && !skip[d.Name()] && strings.HasSuffix(d.Name(), ".go") && !strings.HasSuffix(d.Name(), "_test.go") {
err := normalizeFileComments(path, dry)
if err != nil {
log.Printf("Error processing %s: %v", path, err)
}
}
return nil
})
if err != nil {
log.Fatalf("comment normalization failed: %s", err)
}
}
func normalizeFileComments(filePath string, dry bool) error {
// Validate file path to prevent path traversal attacks
cleanPath := filepath.Clean(filePath)
if !strings.HasPrefix(cleanPath, "solutions/") && !strings.HasPrefix(cleanPath, "./solutions/") {
return fmt.Errorf("invalid file path: %s", filePath)
}
content, err := os.ReadFile(cleanPath) // #nosec G304 - path is validated above
if err != nil {
return err
}
lines := strings.Split(string(content), "\n")
if len(lines) < 2 {
return nil
}
// Find the comment block (starts with /* and ends with */)
startIdx := -1
endIdx := -1
for i, line := range lines {
if strings.Contains(line, "/*") {
startIdx = i
}
if strings.Contains(line, "*/") {
endIdx = i
break
}
}
if startIdx == -1 || endIdx == -1 || startIdx >= endIdx {
return nil // No comment block found
}
// Process each line of the comment block individually
commentLines := lines[startIdx : endIdx+1]
var formattedLines []string
for i, line := range commentLines {
if i == 0 {
// First line with /*
if idx := strings.Index(line, "/*"); idx != -1 {
prefix := line[:idx]
after := line[idx+2:]
if strings.TrimSpace(after) == "" {
formattedLines = append(formattedLines, prefix+"/*")
} else {
// Check if the line is too long and wrap if needed
wrapped := wrapCommentLine(prefix+"/*"+after, 80)
formattedLines = append(formattedLines, wrapped...)
}
}
} else if i == len(commentLines)-1 {
// Last line with */
if idx := strings.Index(line, "*/"); idx != -1 {
before := line[:idx]
suffix := line[idx:]
if strings.TrimSpace(before) == "" {
formattedLines = append(formattedLines, before+suffix)
} else {
// Check if the line is too long and wrap if needed
wrapped := wrapCommentLine(before+suffix, 80)
formattedLines = append(formattedLines, wrapped...)
}
}
} else {
// Middle lines - preserve structure but wrap long lines
if strings.TrimSpace(line) == "" {
// Preserve empty lines
formattedLines = append(formattedLines, line)
} else {
// Wrap long lines while preserving indentation
wrapped := wrapCommentLine(line, 80)
formattedLines = append(formattedLines, wrapped...)
}
}
}
if dry {
fmt.Printf("File: %s\n", filePath)
for _, line := range formattedLines {
fmt.Println(line)
}
fmt.Println(strings.Repeat("-", 80))
return nil
}
// Replace the comment block
newLines := make([]string, 0, len(lines))
newLines = append(newLines, lines[:startIdx]...)
newLines = append(newLines, formattedLines...)
newLines = append(newLines, lines[endIdx+1:]...)
newContent := strings.Join(newLines, "\n")
return os.WriteFile(filePath, []byte(newContent), 0o600)
}
// wrapCommentLine wraps a single comment line if it exceeds maxLength
// while preserving the original indentation
func wrapCommentLine(line string, maxLength int) []string {
if len(line) <= maxLength {
return []string{line}
}
// Extract leading whitespace/indentation
leadingSpace := ""
for i, r := range line {
if r != ' ' && r != '\t' {
leadingSpace = line[:i]
break
}
}
// Get the actual content without leading space
content := strings.TrimLeft(line, " \t")
// If it's still not too long, return as-is
if len(leadingSpace+content) <= maxLength {
return []string{leadingSpace + content}
}
var result []string
words := strings.Fields(content)
if len(words) == 0 {
return []string{line}
}
currentLine := leadingSpace + words[0]
for i := 1; i < len(words); i++ {
word := words[i]
testLine := currentLine + " " + word
if len(testLine) > maxLength {
// Current line is full, start a new one
result = append(result, currentLine)
currentLine = leadingSpace + "\t" + word
} else {
currentLine = testLine
}
}
// Add the last line
if currentLine != leadingSpace {
result = append(result, currentLine)
}
return result
}