Changed sub-pipe to return the line
This commit is contained in:
parent
405bf812ee
commit
19f092b899
2 changed files with 4 additions and 4 deletions
|
|
@ -17,11 +17,11 @@ func eachLine(r io.Reader, fn func(line string) error) error {
|
||||||
return scanner.Err()
|
return scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func SubPipe(fn func(src *script.Pipe) *script.Pipe) func(r io.Reader, w io.Writer) error {
|
func SubPipe(fn func(line string) *script.Pipe) func(r io.Reader, w io.Writer) error {
|
||||||
return func(r io.Reader, w io.Writer) error {
|
return func(r io.Reader, w io.Writer) error {
|
||||||
scanner := bufio.NewScanner(r)
|
scanner := bufio.NewScanner(r)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
bts, err := fn(script.Echo(scanner.Text())).Bytes()
|
bts, err := fn(scanner.Text()).Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@ import (
|
||||||
func TestSubPipe(t *testing.T) {
|
func TestSubPipe(t *testing.T) {
|
||||||
t.Run("should run the subpipe for each line in the parent pipe", func(t *testing.T) {
|
t.Run("should run the subpipe for each line in the parent pipe", func(t *testing.T) {
|
||||||
verifyPipeLines(t, func(p *script.Pipe) *script.Pipe {
|
verifyPipeLines(t, func(p *script.Pipe) *script.Pipe {
|
||||||
return p.Filter(scriptx.SubPipe(func (src *script.Pipe) *script.Pipe {
|
return p.Filter(scriptx.SubPipe(func (line string) *script.Pipe {
|
||||||
return src.Replace("Line", "Subpipe")
|
return script.Echo(line).Replace("Line", "Subpipe")
|
||||||
}))
|
}))
|
||||||
}, []string{
|
}, []string{
|
||||||
"Line 1",
|
"Line 1",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue