-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (74 loc) · 1.8 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
package main
import (
"errors"
"fmt"
"github.com/mi9/refactor/confirm"
"github.com/mi9/refactor/patch"
"github.com/mi9/refactor/termcolor"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
func prettyPrint(filename string, patch *patch.Patch, out io.Writer) {
fmt.Fprintf(out, "%s\n %s\n %s\n", termcolor.Colored(filename, termcolor.Cyan), termcolor.Colored("-"+patch.Before(), termcolor.Red), termcolor.Colored("+"+patch.After(), termcolor.Green))
}
func confirmPatch(filename string, p *patch.Patch, confirmation *confirm.Confirmation) bool {
prettyPrint(filename, p, os.Stdout)
if confirmation.Next() {
return true
}
fmt.Printf("Continue? ([a]ll/[y]es/[n]o (default no): ")
var input string
_, err := fmt.Scanf("%s", &input)
if err != nil {
return false
}
switch input {
case "a":
confirmation.ConfirmAll()
case "y":
confirmation.ConfirmOnce()
default:
return false
}
return confirmation.Next()
}
func main() {
if len(os.Args) <= 3 {
println("Example: refactor .rb require import\n Replaces 'require' with 'import' in all .rb files")
return
}
suffix := os.Args[1]
find := regexp.MustCompile(os.Args[2])
replace := []byte(os.Args[3])
confirmation := new(confirm.Confirmation)
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasPrefix(info.Name(), ".") {
return nil
}
if !strings.HasSuffix(path, suffix) {
return nil
}
patcher := patch.NewPatcher(path, find, replace)
err = patcher.Load()
if err != nil {
return err
}
for p := patcher.Next(); p != nil; p = patcher.Next() {
canProceed := confirmPatch(path, p, confirmation)
if canProceed {
patcher.Accept(p)
} else {
patcher.Done()
return errors.New("refactor aborted by user")
}
}
patcher.Done()
return nil
})
}