-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.go
262 lines (230 loc) · 6.08 KB
/
operators.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package patman
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/dop251/goja"
)
var regexCache = map[string]*regexp.Regexp{}
func regex(pattern string) *regexp.Regexp {
if regex, ok := regexCache[pattern]; ok {
return regex
}
regex, err := regexp.Compile(pattern)
if err != nil {
fmt.Printf("`%s` is not a valid regexp pattern\n", pattern)
os.Exit(1)
}
regexCache[pattern] = regex
return regex
}
type OperatorEntry struct {
Operator operator
Usage string
Alias string
Example string
}
type operator func(line string, arg string) string
var operators = map[string]OperatorEntry{
"match": {
Operator: handleMatch,
Usage: "matches first instance that satisfies expression",
Example: "echo hello | match(e(.*)) # -> ello",
Alias: "m",
},
"m": {
Operator: handleMatch,
},
"matchall": {
Operator: handleMatchAll,
Usage: "matches all instances that satisfy expression",
Example: "echo hello | matchall(l) # -> ll",
Alias: "ma",
},
"ma": {
Operator: handleMatchAll,
},
"replace": {
Operator: handleReplace,
Usage: "replaces expression with provided string",
Example: "echo hello | replace(e/a) # -> hallo",
Alias: "r",
},
"r": {
Operator: handleReplace,
},
"named_replace": {
Operator: handleNamedReplace,
Usage: "replaces expression with provided string. Supports named capture groups",
Example: "echo hello | named_replace(e(?P<first>l)(?P<second>l)o / %second%first) # -> ohell",
Alias: "nr",
},
"nr": {
Operator: handleNamedReplace,
},
"matchline": {
Operator: handleMatchLine,
Usage: "matches entire line that satisfies expression",
Example: "cat test.txt | matchline(hello) # -> ... matching lines",
Alias: "ml",
},
"ml": {
Operator: handleMatchLine,
},
"notmatchline": {
Operator: handleNotMatchLine,
Usage: "returns entire lines that do not match expression",
Example: "cat test.txt | matchline(hello) # -> ... matching lines",
Alias: "nml",
},
"nml": {
Operator: handleNotMatchLine,
},
"split": {
Operator: handleSplit,
Usage: "split line by provided delimiter and take provided index",
Example: "echo 'a b c' | split(\\s/1) # -> b",
Alias: "s",
},
"s": {
Operator: handleSplit,
},
"js": {
Operator: handleJs,
Usage: "execute js expression by passing `x` as argument. returned value is coerced to string",
Example: "echo hello | js(x + 123) # -> hello123",
},
"explode": {
Operator: handleExplode,
Usage: "split line by provided delimiter and join all resulting lines with a \\n (new line) char. Useful for concatenating patman with itself",
Example: "echo 'a b c' | explode(\\s) # -> a\nb\nc",
},
"filter": {
Operator: handleFilter,
Usage: "matches entire line that contains substring. Useful for quickly filtering large files (> 1GB). Way quicker than cat+grep",
Example: "cat logs.txt | match_fast(hello) # -> ... matching lines",
Alias: "mf",
},
"f": {
Operator: handleFilter,
},
}
func Register(name string, o OperatorEntry) {
operators[name] = o
}
func handleMatch(line, arg string) string {
return regex(arg).FindString(line)
}
func handleMatchAll(line, arg string) string {
matches := ""
for _, match := range regex(arg).FindAllString(line, -1) {
matches += match
}
return matches
}
// handleNamedReplace replaces named capture groups in the replacement string.
// e.g. `echo "hello world" | named_replace(e(?P<first>l)(?P<second>l)o / %second%first) # -> ohell`
func handleNamedReplace(line, arg string) string {
// TODO: How to make this useful in case `/` needs to be matched?
cmds := Args(arg)
pattern, replacement := cmds[0], cmds[1]
re := regex(pattern)
// attempt replace with named captures
if strings.Contains(replacement, `%`) {
submatches := re.FindStringSubmatch(line)
names := re.SubexpNames()
if len(submatches) == 0 {
return ""
}
for i, match := range submatches {
if i != 0 && match != "" && names[i] != "" {
replacement = strings.ReplaceAll(replacement, "%"+names[i], match)
}
}
return re.ReplaceAllString(line, replacement)
}
return re.ReplaceAllString(line, replacement)
}
func handleReplace(line, arg string) string {
cmds := Args(arg)
substr, replacement := cmds[0], cmds[1]
return strings.ReplaceAll(line, substr, replacement)
}
func handleMatchLine(line, arg string) string {
if regex(arg).MatchString(line) {
return line
}
return ""
}
func handleFilter(line, arg string) string {
if strings.Contains(line, arg) {
return line
}
return ""
}
func handleNotMatchLine(line, arg string) string {
if !regex(arg).MatchString(line) {
return line
}
return ""
}
func handleSplit(line, arg string) string {
cmds := Args(arg)
pattern, arg := cmds[0], cmds[1]
index, err := strconv.ParseInt(arg, 10, 32)
if err != nil {
fmt.Printf("`%s` is not a valid index\n", arg)
os.Exit(1)
}
parts := regex(pattern).Split(line, -1)
if len(parts)-1 < int(index) {
return ""
}
return parts[index]
}
var vm *goja.Runtime
func handleJs(line, arg string) string {
if vm == nil {
vm = goja.New()
}
// TODO: Should probably escape
vm.RunString(fmt.Sprintf("x = `%s`", line))
script := fmt.Sprintf("String(%s)", arg)
v, err := vm.RunString(script)
if err != nil {
fmt.Println("error while executing js operator:")
fmt.Println(" ", err)
fmt.Println("")
fmt.Println(" ", "arg:", arg)
fmt.Println(" ", "line:", line)
os.Exit(1)
}
return v.Export().(string)
}
func handleExplode(line, arg string) string {
cmds := Args(arg)
pattern, arg := cmds[0], cmds[1]
limit, err := strconv.ParseInt(arg, 10, 32)
if err != nil {
limit = -1
}
parts := regex(pattern).Split(line, int(limit))
return strings.Join(parts, "\n")
}
// Args is a utility used by operators to
// split argument by delimiter. Picking last occurrence.
// e.g. /some/url/replacement -> '/some/url' 'replacement'
func Args(arg string) []string {
// TODO: add configuration for delimiter
parts := strings.Split(arg, "/")
if len(parts) < 2 {
fmt.Printf("missing argument: %v\n", parts)
os.Exit(1)
}
return []string{
strings.Join(parts[0:len(parts)-1], "/"),
parts[len(parts)-1],
}
}