mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-22 07:51:39 -04:00
test: add PipeGrep to grep an input
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user