-
Notifications
You must be signed in to change notification settings - Fork 14
/
extractorBuilder.go
179 lines (163 loc) · 4.75 KB
/
extractorBuilder.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 helpers
import (
"os"
"rare/pkg/expressions"
"rare/pkg/extractor"
"rare/pkg/extractor/batchers"
"rare/pkg/extractor/dirwalk"
"rare/pkg/logger"
"runtime"
"strings"
"github.com/urfave/cli"
)
const DefaultArgumentDescriptor = "<-|filename|glob...>"
func BuildBatcherFromArguments(c *cli.Context) *batchers.Batcher {
var (
follow = c.Bool("follow") || c.Bool("reopen")
followTail = c.Bool("tail")
followReopen = c.Bool("reopen")
followPoll = c.Bool("poll")
concurrentReaders = c.Int("readers")
gunzip = c.Bool("gunzip")
batchSize = c.Int("batch")
recursive = c.Bool("recursive")
)
if batchSize < 1 {
logger.Fatalf("Batch size must be >= 1, is %d", batchSize)
}
if concurrentReaders < 1 {
logger.Fatalf("Must have at least 1 reader")
}
if followPoll && !follow {
logger.Fatalf("Follow (-f) must be enabled for --poll")
}
if followTail && !follow {
logger.Fatalf("Follow (-f) must be enabled for --tail")
}
fileglobs := c.Args()
if len(fileglobs) == 0 || fileglobs[0] == "-" { // Read from stdin
if gunzip {
logger.Fatalln("Cannot decompress (-z) with stdin")
}
if follow {
logger.Println("Cannot follow a stdin stream, not a file")
}
return batchers.OpenReaderToChan("<stdin>", os.Stdin, batchSize)
} else if follow { // Read from source file
if gunzip {
logger.Println("Cannot combine -f and -z")
}
return batchers.TailFilesToChan(dirwalk.GlobExpand(fileglobs, recursive), batchSize, followReopen, followPoll, followTail)
} else { // Read (no-follow) source file(s)
return batchers.OpenFilesToChan(dirwalk.GlobExpand(fileglobs, recursive), gunzip, concurrentReaders, batchSize)
}
}
func BuildExtractorFromArguments(c *cli.Context, batcher *batchers.Batcher) *extractor.Extractor {
return BuildExtractorFromArgumentsEx(c, batcher, expressions.ArraySeparatorString)
}
func BuildExtractorFromArgumentsEx(c *cli.Context, batcher *batchers.Batcher, sep string) *extractor.Extractor {
config := extractor.Config{
Posix: c.Bool("posix"),
Regex: c.String("match"),
Extract: strings.Join(c.StringSlice("extract"), sep),
Workers: c.Int("workers"),
}
if c.Bool("ignore-case") {
config.Regex = "(?i)" + config.Regex
}
ignoreSlice := c.StringSlice("ignore")
if len(ignoreSlice) > 0 {
ignoreExp, err := extractor.NewIgnoreExpressions(ignoreSlice...)
if err != nil {
logger.Fatalln(err)
}
config.Ignore = ignoreExp
}
ret, err := extractor.New(batcher.BatchChan(), &config)
if err != nil {
logger.Fatalln(err)
}
return ret
}
func getExtractorFlags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "follow,f",
Usage: "Read appended data as file grows",
},
cli.BoolFlag{
Name: "reopen,F",
Usage: "Same as -f, but will reopen recreated files",
},
cli.BoolFlag{
Name: "poll",
Usage: "When following a file, poll for changes rather than using inotify",
},
cli.BoolFlag{
Name: "tail,t",
Usage: "When following a file, navigate to the end of the file to skip existing content",
},
cli.BoolFlag{
Name: "posix,p",
Usage: "Compile regex as against posix standard",
},
cli.StringFlag{
Name: "match,m",
Usage: "Regex to create match groups to summarize on",
Value: ".*",
},
cli.StringSliceFlag{
Name: "extract,e",
Usage: "Expression that will generate the key to group by. Specify multiple times for multi-dimensions or use {$} helper",
Value: &cli.StringSlice{"{0}"},
},
cli.BoolFlag{
Name: "gunzip,z",
Usage: "Attempt to decompress file when reading",
},
cli.IntFlag{
Name: "batch",
Usage: "Specifies io batching size. Set to 1 for immediate input",
Value: 1000,
},
cli.IntFlag{
Name: "workers,w",
Usage: "Set number of data processors",
Value: runtime.NumCPU()/2 + 1,
},
cli.IntFlag{
Name: "readers,wr",
Usage: "Sets the number of concurrent readers (Infinite when -f)",
Value: 3,
},
cli.StringSliceFlag{
Name: "ignore,i",
Usage: "Ignore a match given a truthy expression (Can have multiple)",
},
cli.BoolFlag{
Name: "recursive,R",
Usage: "Recursively walk a non-globbing path and search for plain-files",
},
cli.BoolFlag{
Name: "ignore-case,I",
Usage: "Augment regex to be case insensitive",
},
}
}
func AdaptCommandForExtractor(command cli.Command) *cli.Command {
command.Flags = append(getExtractorFlags(), command.Flags...)
if command.ArgsUsage == "" {
command.ArgsUsage = DefaultArgumentDescriptor
}
// While this doesn't own the log, this is the last place
// that has the option to flush the log buffer to sderr
originalAfter := command.After
command.After = func(c *cli.Context) error {
logger.ImmediateLogs()
if originalAfter != nil {
return originalAfter(c)
}
return nil
}
return &command
}