-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.go
181 lines (165 loc) · 5 KB
/
pipeline.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package pipeline
import (
"bufio"
"io"
"os"
"sync"
)
// A pipeline consisting of stations that takes two input streams and produce two output streams
type Pipeline struct {
stdout <-chan string
stderr <-chan string
chainable bool
err error
prev *Pipeline
}
// Create a new pipeline that has not input streams
func NewPipeline() *Pipeline {
ch1, ch2 := make(chan string), make(chan string)
p := &Pipeline{stdout: ch1, stderr: ch2, chainable: true}
close(ch1)
close(ch2)
return p
}
// Create a new pipeline with input streams stdout and stderr
func StartPipelineWithStreams(stdout <-chan string, stderr <-chan string) *Pipeline {
return &Pipeline{stdout: stdout, stderr: stderr, chainable: true, prev: nil}
}
// Create a new pipeline whose input streams are from a command output
func StartPipelineWithCommand(name string, arg ...interface{}) *Pipeline {
out, err := make(chan string, 4), make(chan string, 4)
p := &Pipeline{stdout: out, stderr: err, chainable: true, prev: nil}
ch1, ch2 := make(chan string), make(chan string)
var station Station
if len(arg) == 0 {
station = newCommand(name, &p.err)
} else {
station = newCommand(name, &p.err, arg...)
}
station.SetStreams(ch1, ch2, out, err)
close(ch1)
close(ch2)
station.Start()
return p
}
// Create a new pipeline whose input streams are /dev/stdin and /dev/stdout
func StartPipelineWithStdin() *Pipeline {
out, err := make(chan string, 4), make(chan string, 4)
p := &Pipeline{stdout: out, stderr: err, chainable: true, prev: nil}
close(err)
go func() {
bufin := bufio.NewReader(os.Stdin)
var er error
str := make([]byte, _UNITSIZE)
var nread int
for er == nil {
nread, er = bufin.Read(str)
if er == nil || er == io.EOF {
out <- string(str[:nread])
}
}
close(out)
}()
return p
}
// Add a command station at the end of the pipeline, i.e., start a command, chain the output streams of current pipeline to the input streams of a the command, and let the output stremas of the command be the new output streams of the pipeline
func (p *Pipeline) ChainCommand(name string, arg ...interface{}) *Pipeline {
if !p.chainable {
panic("pipeline is not chainable")
}
out, err := make(chan string, 4), make(chan string, 4)
r := &Pipeline{stdout: out, stderr: err, chainable: true, prev: p}
var station Station
if len(arg) == 0 {
station = newCommand(name, &p.err)
} else {
station = newCommand(name, &p.err, arg...)
}
station.SetStreams(p.stdout, p.stderr, out, err)
station.Start()
return r
}
// Add a line processing station at the end of the pipeline. The line processing station process the input streams line by line. The output stremas of the line processing station will be the new output streams of the pipeline.
func (p *Pipeline) ChainLineProcessor(stdoutProcessor LineProcessor, stderrProcessor LineProcessor) *Pipeline {
if !p.chainable {
panic("pipeline is not chainable")
}
out, err := make(chan string, 4), make(chan string, 4)
r := &Pipeline{stdout: out, stderr: err, chainable: true, prev: p}
station := newLineProcessorStation(stdoutProcessor, stderrProcessor)
station.SetStreams(p.stdout, p.stderr, out, err)
station.Start()
return r
}
func (p *Pipeline) RedirectTo(to int) *Pipeline {
if !p.chainable {
panic("pipeline is not chainable")
}
out, err := make(chan string, 4), make(chan string, 4)
r := &Pipeline{stdout: out, stderr: err, chainable: true, prev: p}
station := newRedirectStation(to)
station.SetStreams(p.stdout, p.stderr, out, err)
station.Start()
return r
}
// Seal the pipeline (prevent from chaining new stations into the pipeline) and print current output streams of the pipeline to /dev/stdout and /dev/stderr respectively
func (p *Pipeline) PrintAll() {
if !p.chainable {
panic("pipline is not chainable")
}
p.chainable = false
var wg sync.WaitGroup
h := func(ch_in <-chan string, out *os.File) {
ok := true
var str string
for ok {
str, ok = <-ch_in
if ok {
out.WriteString(str)
}
}
wg.Done()
}
wg.Add(2)
go h(p.stdout, os.Stdout)
go h(p.stderr, os.Stderr)
wg.Wait()
}
// Seal the pipeline (prevent from chaining new stations into the pipeline) and return the two output streams
func (p *Pipeline) GetOutputStreams() (stdout <-chan string, stderr <-chan string) {
stdout = p.stdout
stderr = p.stderr
p.chainable = false
return
}
func (p *Pipeline) Errors() []error {
var errs []error
for it := p; it != nil; it = it.prev {
if it.err != nil {
errs = append(errs, p.err)
}
}
return errs
}
// Short for ChainCommand()
func (p *Pipeline) C(name string, arg ...interface{}) *Pipeline {
var r *Pipeline
if len(arg) == 0 {
r = p.ChainCommand(name)
} else {
r = p.ChainCommand(name, arg...)
}
return r
}
// Short for ChainLineProcessor()
func (p *Pipeline) L(stdoutProcessor LineProcessor, stderrProcessor LineProcessor) *Pipeline {
return p.ChainLineProcessor(stdoutProcessor, stderrProcessor)
}
// Short for PrintAll()
func (p *Pipeline) P() {
p.PrintAll()
}
// Short for RedirectTo()
func (p *Pipeline) R(to int) *Pipeline {
return p.RedirectTo(to)
}