test: add PipeGrep to grep an input

This commit is contained in:
Lucas Bajolet
2024-06-07 14:38:20 -04:00
parent c3e4c5cde4
commit bd79b37aa7
+15 -1
View File
@@ -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