/
passthrough.go
52 lines (43 loc) · 1.23 KB
/
passthrough.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
passthrough allows you to pass files through an executable program.
The program is executed for every single file, If you want to pass a series
of files through a single invocation of the program, please use slurp.Concat
and pipe it to passtrhough to be processed by your designed program.
*/
package passthrough
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"github.com/omeid/gonzo"
"github.com/omeid/gonzo/context"
)
// bin is the binary name, it will be passed to os/exec.Command, so the same
// path rules applies.
// the args are the argumetns passed to the program.
func Run(bin string, args ...string) gonzo.Stage {
return func(ctx context.Context, in <-chan gonzo.File, out chan<- gonzo.File) error {
for {
select {
case file, ok := <-in:
if !ok {
return nil
}
cmd := exec.Command(bin, args...)
cmd.Stderr = os.Stderr //TODO: io.Writer logger.
cmd.Stdin = file
ctx = context.WithValue(ctx, "cmd", bin)
ctx.Infof("Passing %s", file.FileInfo().Name())
output, err := cmd.Output()
if err != nil {
return err
}
content := ioutil.NopCloser(bytes.NewReader(output))
out <- gonzo.NewFile(content, file.FileInfo())
case <-ctx.Done():
return ctx.Err()
}
}
}
}