bitfield/script

Feature Request: Add FilterFunc

gmlewis opened this issue · 2 comments

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 { ... }

Ah! I think FilterScan can do what I want.
Thanks.
Closing.

@gmlewis there are three ways to write a custom filter:

  1. FilterLine is for when every line of input should produce a line of output (for example, some kind of text transformation).
  2. 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).
  3. Filter is the generic case where you can Read as much as you want from upstream, and Write as much you want downstream, and the rest is up to you.