-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinvocation.go
181 lines (170 loc) · 4.51 KB
/
invocation.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 someutils
import (
"bytes"
"errors"
"io"
"os"
"time"
)
// A set of invocation (In, Out, ErrOut, and even ErrIn (but ErrIn is usually only used by the special 'Redirector' util)
// Note that Pipables are not expected to use this type (Pipables should not need any dependency on someutils - just the implicit implementation of the Pipable interface)
type Invocation struct {
Pipeline *Pipeline
Pipable Pipable
MainPipe *Pipe
ErrPipe *Pipe
/*
MainPipe.In io.Reader
MainPipe.Out io.Writer
ErrPipe.In io.Reader
ErrPipe.Out io.Writer
*/
SignalReceiver chan Signal
ExitCode *int
Err error
Closed bool
doneChan chan bool
}
/*
func (i *Invocation) AutoPipeErrInOut() {
go autoPipe(i.ErrPipe.Out, i.ErrPipe.In)
}
*/
//TODO!!
func (i *Invocation) AutoHandleSignals() {
// go handleSignals(i)
}
func (i *Invocation) Pipe(pipable Pipable) (error, int) {
if i.Pipable != nil {
return errors.New("This invocation already was already invoked!"), 1
}
i.Pipable = pipable
return pipable.Invoke(i)
}
func (i *Invocation) WaitUpTo(timeout time.Duration) (error, *int) {
start := time.Now()
for {
diff := time.Now().Sub(start)
select {
//todo offset timeout against time already spent in previous iterations
case <-time.After(timeout - diff):
return errors.New("Timeout waiting for exit codes"), nil
case _, ok := <-i.doneChan:
if !ok {
return nil, i.ExitCode
}
}
}
}
func (i *Invocation) Wait() *int {
for {
_, ok := <-i.doneChan
if !ok {
return i.ExitCode
}
}
}
/*
func (i *Invocation) PipeToPipeline(pipeline *Pipeline) (error, chan *Invocation, int) {
if i.Pipeline != nil {
return errors.New("This invocation already was already invoked!"), nil, 0
}
i.Pipeline = pipeline
invocationChannel, count := pipeline.Invoke(i)
return nil, invocationChannel, count
}
*/
//warning. This closes a channel. Don't call it twice - panic ensues!
func (i *Invocation) Close() error {
if i.Closed {
return nil
}
i.Closed = true
close(i.doneChan) //this channel should only be used to check if done.
i.MainPipe.CloseIfClosers()
i.ErrPipe.CloseIfClosers()
/*
closer, ok := i.MainPipe.In.(io.Closer)
if ok {
err := closer.Close()
//never mind
if err != nil {
fmt.Fprintln(os.Stderr, "Could not close inPipe (", err, ")")
}
} else {
// fmt.Fprintln(os.Stderr, "inPipe not a Closer")
}
closer, ok = i.MainPipe.Out.(io.Closer)
if ok {
err := closer.Close()
//never mind
if err != nil {
fmt.Fprintln(os.Stderr, "Could not close outPipe (", err, ")")
}
} else {
// fmt.Fprintln(os.Stderr, "outPipe not a Closer")
}
closer, ok = i.ErrPipe.In.(io.Closer)
if ok {
err := closer.Close()
//never mind
if err != nil {
fmt.Fprintln(os.Stderr, "Could not close errMainPipe.In (", err, ")")
}
} else {
// fmt.Fprintln(os.Stderr, "errMainPipe.In not a Closer")
}
closer, ok = i.ErrPipe.Out.(io.Closer)
if ok {
err := closer.Close()
//never mind
if err != nil {
fmt.Fprintln(os.Stderr, "Could not close errMainPipe.Out (", err, ")")
}
} else {
// fmt.Fprintln(os.Stderr, "errMainPipe.Out not a Closer")
}
*/
/*
i.SignalReceiver <- 9
*/
close(i.SignalReceiver)
return nil
}
// Convenience method returns the Stdin/Stdout/Stderr invocation associated with this process
func StdInvocation() *Invocation {
ret := NewInvocation(os.Stdin, os.Stdout, os.Stderr)
return ret
}
//convenience method for CLI handlers
func StdInvoke(pcu CliPipable, call []string) (error, int) {
invocation := StdInvocation()
err, code := pcu.ParseFlags(call, invocation.ErrPipe.Out)
if err != nil {
return err, code
}
return invocation.Pipe(pcu)
}
// Factory for a pipeline with the given invocation
func NewInvocation(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer) *Invocation {
i := new(Invocation)
i.init()
i.MainPipe.In = inPipe
i.MainPipe.Out = outPipe
i.ErrPipe.In = new(bytes.Reader)
i.ErrPipe.Out = errPipe
return i
}
func (i *Invocation) init() {
i.SignalReceiver = make(chan Signal)
i.doneChan = make(chan bool)
i.MainPipe = new(Pipe)
i.ErrPipe = new(Pipe)
}
// Factory taking a string for Stdin, and using byte buffers for the sdout and stderr invocation
// This returns the byte buffers to avoid the need to cast. (The assumption being that you'll want to call .Bytes() or .String() on those buffers)
func InvocationFromReader(inPipe io.Reader) (*Invocation, *bytes.Buffer, *bytes.Buffer) {
outPipe := new(bytes.Buffer)
errPipe := new(bytes.Buffer)
return NewInvocation(inPipe, outPipe, errPipe), outPipe, errPipe
}