/
main.go
244 lines (200 loc) · 5.41 KB
/
main.go
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
package main
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/jessevdk/go-flags"
"github.com/mgutz/ansi"
)
var styleRed = ansi.ColorFunc("red")
var styleRedUnderline = ansi.ColorFunc("red+u")
var styleGreen = ansi.ColorFunc("green")
var styleGreenUnderline = ansi.ColorFunc("green+u")
var styleHeader = ansi.ColorFunc("white+b:black")
var styleBold = ansi.ColorFunc("+b")
func main() {
dir, _ := os.Getwd()
exitCode := mainSub(dir, os.Stdout, os.Stdin, os.Args[1:])
if exitCode != 0 {
os.Exit(exitCode)
}
}
type options struct {
DryRun bool `short:"d" long:"dry-run" description:"Do not change anything"`
Regexp bool `short:"r" long:"regexp" description:"Treat search string as regular expression"`
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Interactive bool `short:"i" long:"interactive" description:"Confirm every replacement"`
Args struct {
Search string
Replace string
} `positional-args:"yes" required:"yes"`
}
func mainSub(workingDir string, stdout io.Writer, stdin io.Reader, args []string) int {
output := &Output{
stdout: stdout,
verbose: false,
}
opts, exitCode := parseOptions(output, args)
if opts == nil {
return exitCode
}
output.verbose = opts.Verbose
filter := NewFilter(workingDir)
finder := &Finder{
output: output,
filter: filter,
}
program := &Program{
Output: output,
Finder: finder,
RootDirectory: workingDir,
Stdout: stdout,
Stdin: stdin,
// args
Search: opts.Args.Search,
Replace: opts.Args.Replace,
// options
DryRun: opts.DryRun,
Verbose: opts.Verbose,
Regexp: opts.Regexp,
Interactive: opts.Interactive,
}
program.Execute()
return 0
}
type Program struct {
Output *Output
Finder *Finder
RootDirectory string
Stdout io.Writer
Stdin io.Reader
Search string
Replace string
DryRun bool
Verbose bool
Regexp bool
Interactive bool
}
func (p *Program) Execute() {
p.Output.reportVerbose(
"(search: %s, replace: %s, dry-run: %v, regexp: %v)",
p.Search, p.Replace, p.DryRun, p.Regexp)
p.Output.reportVerbose("Root-Directory: %s", p.RootDirectory)
if p.Regexp {
_, err := regexp.Compile(p.Search)
if err != nil {
p.Output.reportError(
"Could not compile regular expression: %s - %s",
p.Search, err)
return
}
}
replace := &Replace{
Search: p.Search,
Replace: p.Replace,
Regexp: p.Regexp,
}
ask := &Ask{
Stdin: p.Stdin,
Stdout: p.Stdout,
}
entries := p.Finder.Find(p.RootDirectory)
// iterate reversed, so directories are renamed after files are written
for i := len(entries) - 1; i >= 0; i-- {
path := entries[i]
p.Output.reportVerbose(
"Processing(%d/%d) %s...", len(entries)-i, len(entries), p.shortenPath(path))
file, err := os.Open(path)
if err != nil {
p.Output.reportError("Could not open: %s (%s)", p.shortenPath(path), err)
continue
}
fileInfo, err := file.Stat()
if err != nil {
p.Output.reportError("Could not stat: %s (%s)", p.shortenPath(path), err)
continue
}
// close file directly (no defer) to prevent to many open files error
file.Close()
// Step 1 - Replace search string in files content
if !fileInfo.IsDir() {
bytes, err := ioutil.ReadFile(path)
if err != nil {
p.Output.reportError("Could not read: %s (%s)", p.shortenPath(path), err)
continue
}
matchCount := 0
content := string(bytes)
newContent := replace.Execute(content, func(info ReplacementInfo) bool {
matchCount++
p.Output.printHeader("Match #%d in %s", matchCount, p.shortenPath(path))
p.Output.reportReplacement(info)
if p.Interactive && !ask.question(styleBold("Replace?")) {
return false
}
return true
})
if newContent != content {
p.Output.reportInfo("Write: %s", p.shortenPath(path))
if !p.DryRun {
err = ioutil.WriteFile(path, []byte(newContent), fileInfo.Mode())
if err != nil {
p.Output.reportError(
"Could not write: %s (%s)", p.shortenPath(path), err)
continue
}
}
}
}
// Step 2 - Replace search string in file or directory name
baseName := filepath.Base(path)
newName := replace.Execute(baseName, func(info ReplacementInfo) bool {
p.Output.printHeader("Rename %s to %s", p.shortenPath(path), info.ReplLine)
if p.Interactive && !ask.question(styleBold("Rename?")) {
return false
}
return true
})
if newName != baseName {
newPath := filepath.Join(filepath.Dir(path), newName)
p.Output.reportInfo("Rename: %s", p.shortenPath(newPath))
if !p.DryRun {
err = os.Rename(path, newPath)
if err != nil {
p.Output.reportError("Could not move: %s (%s)", p.shortenPath(path), err)
continue
}
}
}
}
return
}
func (p *Program) shortenPath(path string) string {
return strings.Replace(path, p.RootDirectory+"/", "", 1)
}
func parseOptions(output *Output, args []string) (*options, int) {
opts := options{}
parser := flags.NewParser(&opts, flags.PassDoubleDash|flags.HelpFlag)
args, err := parser.ParseArgs(args)
if err != nil {
if parserErr, ok := err.(*flags.Error); ok {
if parserErr.Type != flags.ErrHelp {
output.printf("%s\n\n", parserErr.Message)
}
var b bytes.Buffer
parser.WriteHelp(&b)
output.printf("%s", b.String())
if parserErr.Type == flags.ErrHelp {
return nil, 0
}
} else {
panic(err)
}
return nil, 2
}
return &opts, 0
}