Feature Request: Add FilterFunc
gmlewis opened this issue · 2 comments
gmlewis commented
Proposal
I would like to add the following function to the repo:
func (p *Pipe) FilterFunc(filter func(string) bool) *Pipe
It only retains lines where the filter
func returns true
.
Use Case
I want to write a script that performs a search-and-replace in dozens of Yaml and Go files that exist either in a "/.github/workflows/" or a "/cicd/" subdirectory. Using the existing "MatchRegexp" and/or "RejectRegexp" pipe
methods is a pain. It would be much easier to write something like this (which is a more familiar
functional-programming definition of a "filter"):
script.FindFiles(".").FilterFunc(isCICDFile).Stdout()
where isCICDFile
is written by the user:
func isCICDFile(filename string) bool { ... }
gmlewis commented
Ah! I think FilterScan
can do what I want.
Thanks.
Closing.
bitfield commented
@gmlewis there are three ways to write a custom filter:
FilterLine
is for when every line of input should produce a line of output (for example, some kind of text transformation).FilterScan
is for when a line of input may or may not produce a line of output (for example, collation or reduction of some kind).Filter
is the generic case where you canRead
as much as you want from upstream, andWrite
as much you want downstream, and the rest is up to you.