diff options
author | Ethan Reesor <ethan.reesor@gmail.com> | 2020-06-05 22:31:57 +0300 |
---|---|---|
committer | Ethan Reesor <ethan.reesor@gmail.com> | 2020-07-02 10:12:24 +0300 |
commit | a40607fd4a19ec6973fef24266fb5a20b55e1a57 (patch) | |
tree | 444800c7f8249ee1699ddb27aa7a1cc5ae6b4695 /internal/helper/lines | |
parent | 1d0a0d0ca162696b76ee7b410430c3d5e818c6cf (diff) |
Add regex-based filtering to SearchFilesByName
Diffstat (limited to 'internal/helper/lines')
-rw-r--r-- | internal/helper/lines/send.go | 11 | ||||
-rw-r--r-- | internal/helper/lines/send_test.go | 9 |
2 files changed, 19 insertions, 1 deletions
diff --git a/internal/helper/lines/send.go b/internal/helper/lines/send.go index 65fd2c019..d0b65d067 100644 --- a/internal/helper/lines/send.go +++ b/internal/helper/lines/send.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "io" + "regexp" ) // SenderOpts contains fields that Send() uses to determine what is considered @@ -22,6 +23,9 @@ type SenderOpts struct { // will be called with an empty slice previous to sending the first line // in order to allow sending everything right from the beginning. IsPageToken func([]byte) bool + // Filter limits sent results to those that pass the filter. The zero + // value (nil) disables filtering. + Filter *regexp.Regexp } // ItemsPerMessage establishes the threshold to flush the buffer when using the @@ -117,6 +121,10 @@ func (w *writer) consume(r io.Reader) error { pastPageToken = w.options.IsPageToken(line) continue } + + if w.filter() != nil && !w.filter().Match(line) { + continue + } i++ // Only increment the counter if the result wasn't skipped if err := w.addLine(line); err != nil { @@ -127,7 +135,8 @@ func (w *writer) consume(r io.Reader) error { return w.flush() } -func (w *writer) delimiter() []byte { return w.options.Delimiter } +func (w *writer) delimiter() []byte { return w.options.Delimiter } +func (w *writer) filter() *regexp.Regexp { return w.options.Filter } // Send reads output from `r`, splits it at `opts.Delimiter`, then handles the // buffered lines using `sender`. diff --git a/internal/helper/lines/send_test.go b/internal/helper/lines/send_test.go index 523d822ac..64b763a9b 100644 --- a/internal/helper/lines/send_test.go +++ b/internal/helper/lines/send_test.go @@ -2,6 +2,7 @@ package lines import ( "bytes" + "regexp" "testing" "github.com/stretchr/testify/require" @@ -16,6 +17,7 @@ func TestLinesSend(t *testing.T) { tcs := []struct { desc string + filter *regexp.Regexp limit int isPageToken func([]byte) bool output [][]byte @@ -36,6 +38,12 @@ func TestLinesSend(t *testing.T) { output: expected[0:2], }, { + desc: "filter and limit", + limit: 1, + filter: regexp.MustCompile("foo"), + output: expected[1:2], + }, + { desc: "skip lines", limit: 100, isPageToken: func(line []byte) bool { return bytes.HasPrefix(line, expected[0]) }, @@ -58,6 +66,7 @@ func TestLinesSend(t *testing.T) { err := Send(reader, sender, SenderOpts{ Limit: tc.limit, IsPageToken: tc.isPageToken, + Filter: tc.filter, }) require.NoError(t, err) require.Equal(t, tc.output, out) |