workgroups/vendor/github.com/posener/script/grep.go
Marvin Preuss f121b05b58
Some checks failed
continuous-integration/drone/push Build is failing
repo: adds goreadme
2021-09-24 18:29:05 +02:00

41 lines
820 B
Go

package script
import (
"fmt"
"regexp"
)
// Grep filters only line that match the given regexp.
//
// Shell command: `grep <re>`.
func (s Stream) Grep(re *regexp.Regexp) Stream {
return s.Modify(Grep{Re: re})
}
// Grep is a modifier that filters only line that match `Re`. If Invert was set only line that did
// not match the regex will be returned.
//
// Usage:
//
// (<Stream object>).Modify(script.Grep{Re: <re>})
//
// Shell command: `grep [-v <Invert>] <Re>`.
type Grep struct {
Re *regexp.Regexp
Inverse bool
}
func (g Grep) Modify(line []byte) (modifed []byte, err error) {
if line == nil {
return nil, nil
}
if g.Re.Match(line) != g.Inverse {
return append(line, '\n'), nil
}
return nil, nil
}
func (g Grep) Name() string {
return fmt.Sprintf("grep(%v, invert=%v)", g.Re, g.Inverse)
}