test: add PipeGrep to grep an input

pull/12983/head
Lucas Bajolet 2 years ago
parent c3e4c5cde4
commit bd79b37aa7

@ -1,6 +1,10 @@
package test
import "fmt"
import (
"fmt"
"regexp"
"strings"
)
// Pipe is any command that allows piping two gadgets together
//
@ -17,6 +21,16 @@ func (c CustomPipe) Process(input string) (string, error) {
return c(input)
}
// PipeGrep performs a grep on an input and returns the matches, one-per-line.
//
// The expression passed as parameter will be compiled to a POSIX extended regexp.
func PipeGrep(expression string) Pipe {
re := regexp.MustCompilePOSIX(expression)
return CustomPipe(func(input string) (string, error) {
return strings.Join(re.FindAllString(input, -1), "\n"), nil
})
}
// Tester is the end of a pipe for testing purposes.
//
// Once multiple commands have been piped together in a pipeline, we can

Loading…
Cancel
Save