-
Notifications
You must be signed in to change notification settings - Fork 0
/
goreplace.go
179 lines (139 loc) · 3.3 KB
/
goreplace.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/manifoldco/promptui"
"github.com/urfave/cli"
)
var directoriesToSkip = []string{
"node_modules",
".git",
}
var startingDirectory string
var searchRecursively bool
var whatToSearch string
var extensionsToSearch string
var whatToSearchFor string
var replacement string
func main() {
app := cli.NewApp()
app.Name = "goreplace"
app.Usage = "CLI application for replacing text in files"
app.Action = func(c *cli.Context) error {
promptForStartingDirectory()
// promptForRecursiveSearch()
promptForWhatToSearch()
if whatToSearch == "Specific extensions" {
promptForExtensionsToSearch()
}
promptForWhatToSearchFor()
promptForReplacement()
fmt.Printf("\n\nSTARTING: \n")
perform()
return nil
}
app.Run(os.Args)
}
func promptForStartingDirectory() {
prompt := promptui.Prompt{
Label: "Enter the directory to start searching in: ",
}
startingDirectory, _ = prompt.Run()
}
func promptForRecursiveSearch() {
selection := ""
searchRecursively = false
prompt := promptui.Select{
Label: "Search for files recursively? ",
Items: []string{
"Yes",
"No",
},
}
_, selection, _ = prompt.Run()
if selection == "Yes" {
searchRecursively = true
}
}
func promptForWhatToSearch() {
prompt := promptui.Select{
Label: "Search: ",
Items: []string{
"All files",
"Specific extensions",
},
}
_, whatToSearch, _ = prompt.Run()
}
func promptForExtensionsToSearch() {
prompt := promptui.Prompt{
Label: "Extensions to search (separated by comma, include period): ",
}
extensionsToSearch, _ = prompt.Run()
}
func promptForWhatToSearchFor() {
prompt := promptui.Prompt{
Label: "Enter what to search for (regex): ",
}
whatToSearchFor, _ = prompt.Run()
}
func promptForReplacement() {
prompt := promptui.Prompt{
Label: "Enter replacement string: ",
}
replacement, _ = prompt.Run()
}
func perform() error {
extensions := strings.Split(extensionsToSearch, ",")
err := filepath.Walk(startingDirectory, func(path string, info os.FileInfo, err error) error {
var fileContents []byte
continueProcessing := false
if err != nil {
fmt.Printf("Error accessing path %q: %v\n", path, err)
return err
}
if info.IsDir() {
for _, dirToSkip := range directoriesToSkip {
if info.Name() == dirToSkip {
fmt.Printf("Skipping directory %s\n", info.Name())
return nil
}
}
}
if whatToSearch == "Specific extensions" {
if !info.IsDir() {
for _, extension := range extensions {
fileExtension := filepath.Ext(info.Name())
if fileExtension == extension {
continueProcessing = true
}
}
if !continueProcessing {
return nil
}
} else {
return nil
}
}
//fullPath := filepath.Join(path, info.Name())
if fileContents, err = ioutil.ReadFile(path); err != nil {
fmt.Printf("Error reading file %q: %v\n", path, err)
return err
}
r := regexp.MustCompile(whatToSearchFor)
if r.Match(fileContents) {
newFileContents := r.ReplaceAll(fileContents, []byte(replacement))
if err = ioutil.WriteFile(path, newFileContents, info.Mode()); err != nil {
fmt.Printf("Error writing updated file %s: %v\n", path, err)
return err
}
fmt.Printf("File %s updated\n", path)
}
return nil
})
return err
}