Repository navigation
Expand file tree
/
Copy pathprocessor.go
More file actions
124 lines (108 loc) · 3.59 KB
/
Copy pathprocessor.go
File metadata and controls
124 lines (108 loc) · 3.59 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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
func processFile(filename string, forceReplace bool, re *regexp.Regexp) {
file, err := os.Open(filename)
if err != nil {
printError("Error opening file %s: %v", filename, err)
return
}
defer file.Close()
var lines []string
var replacements []struct {
lineNum int
original string
replaced string
}
scanner := bufio.NewScanner(file)
lineNum := 0
found := false
for scanner.Scan() {
lineNum++
line := scanner.Text()
matches := re.FindAllStringSubmatch(line, -1)
if len(matches) > 0 {
found = true
for _, match := range matches {
if len(match) == 3 {
originalText := match[0]
variable := match[1]
field := match[2]
replacementText := fmt.Sprintf("(*%s).%s", variable, field)
printWarning(filename, lineNum, originalText, line)
replacements = append(replacements, struct {
lineNum int
original string
replaced string
}{lineNum: lineNum, original: originalText, replaced: replacementText})
}
}
}
lines = append(lines, line)
}
if err := scanner.Err(); err != nil {
printError("Error reading file %s: %v", filename, err)
return
}
if !found {
printInfo("No '->' patterns found in %s.", filename)
return
}
madeChanges := false
linesToModify := make(map[int]string)
for _, r := range replacements {
apply := forceReplace
if !forceReplace {
promptMsg := fmt.Sprintf("Replace '%s' with '%s' in %s:%d? (y/N): ", r.original, r.replaced, filename, r.lineNum)
if promptUser(promptMsg) {
apply = true
}
}
if apply {
madeChanges = true
currentLine, ok := linesToModify[r.lineNum]
if !ok {
currentLine = lines[r.lineNum-1]
}
linesToModify[r.lineNum] = strings.Replace(currentLine, r.original, r.replaced, 1)
}
}
if madeChanges {
printInfo("Applying changes to %s...", filename)
var outputLines []string
for i, originalLine := range lines {
lineNumber := i + 1
if modifiedLine, ok := linesToModify[lineNumber]; ok {
outputLines = append(outputLines, modifiedLine)
} else {
outputLines = append(outputLines, originalLine)
}
}
outFile, err := os.Create(filename)
if err != nil {
printError("Error opening file %s for writing: %v", filename, err)
return
}
defer outFile.Close()
writer := bufio.NewWriter(outFile)
for _, line := range outputLines {
if _, err := writer.WriteString(line + "\n"); err != nil {
printError("Error writing to file %s: %v", filename, err)
_ = writer.Flush()
return
}
}
if err = writer.Flush(); err != nil {
printError("Error flushing writer for file %s: %v", filename, err)
return
}
printSuccess("Changes saved to %s.", filename)
} else {
printInfo("No changes made to %s.", filename)
}
}