-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
207 lines (165 loc) · 4.53 KB
/
response.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package legacy
import (
"context"
"io"
"os"
"reflect"
"sync"
"gx/ipfs/QmTjNRVt2fvaRFu93keEC7z5M1GS1iH6qZ9227htQioTUY/go-ipfs-cmds"
"gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
oldcmds "github.com/ipfs/go-ipfs/commands"
)
// responseWrapper wraps Response and implements olcdms.Response.
// It embeds a Response so some methods are taken from that.
type responseWrapper struct {
cmds.Response
out interface{}
}
// Request returns a (faked) oldcmds.Request
func (rw *responseWrapper) Request() oldcmds.Request {
return &requestWrapper{rw.Response.Request(), nil}
}
// Output returns either a <-chan interface{} on which you can receive the
// emitted values, or an emitted io.Reader
func (rw *responseWrapper) Output() interface{} {
//if not called before
if rw.out == nil {
// get first emitted value
x, err := rw.Next()
if err != nil {
return nil
}
if e, ok := x.(*cmdkit.Error); ok {
ch := make(chan interface{})
log.Error(e)
close(ch)
return (<-chan interface{})(ch)
}
switch v := x.(type) {
case io.Reader:
// if it's a reader, set it
rw.out = v
case cmds.Single:
rw.out = v.Value
default:
// if it is something else, create a channel and copy values from next in there
ch := make(chan interface{})
rw.out = (<-chan interface{})(ch)
go func() {
defer close(ch)
ch <- v
for {
v, err := rw.Next()
if err == io.EOF || err == context.Canceled {
return
}
if err != nil {
log.Error(err)
return
}
ch <- v
}
}()
}
}
// if we have it already, return existing value
return rw.out
}
// SetError is an empty stub
func (rw *responseWrapper) SetError(error, cmdkit.ErrorType) {}
// SetOutput is an empty stub
func (rw *responseWrapper) SetOutput(interface{}) {}
// SetLength is an empty stub
func (rw *responseWrapper) SetLength(uint64) {}
// SetCloser is an empty stub
func (rw *responseWrapper) SetCloser(io.Closer) {}
// Close is an empty stub
func (rw *responseWrapper) Close() error { return nil }
// Marshal is an empty stub
func (rw *responseWrapper) Marshal() (io.Reader, error) { return nil, nil }
// Reader is an empty stub
func (rw *responseWrapper) Reader() (io.Reader, error) { return nil, nil }
// Stdout returns os.Stdout
func (rw *responseWrapper) Stdout() io.Writer { return os.Stdout }
// Stderr returns os.Stderr
func (rw *responseWrapper) Stderr() io.Writer { return os.Stderr }
// fakeResponse implements oldcmds.Response and takes a ResponseEmitter
type fakeResponse struct {
req oldcmds.Request
re cmds.ResponseEmitter
out interface{}
wait chan struct{}
once sync.Once
}
// Send emits the value(s) stored in r.out on the ResponseEmitter
func (r *fakeResponse) Send(errCh chan<- error) {
defer close(errCh)
out := r.Output()
if out == nil {
return
}
if ch, ok := out.(chan interface{}); ok {
out = (<-chan interface{})(ch)
}
err := r.re.Emit(out)
errCh <- err
return
}
// Request returns the oldcmds.Request that belongs to this Response
func (r *fakeResponse) Request() oldcmds.Request {
return r.req
}
// SetError forwards the call to the underlying ResponseEmitter
func (r *fakeResponse) SetError(err error, code cmdkit.ErrorType) {
defer r.once.Do(func() { close(r.wait) })
r.re.SetError(err, code)
}
// Error is an empty stub
func (r *fakeResponse) Error() *cmdkit.Error {
return nil
}
// SetOutput sets the output variable to the passed value
func (r *fakeResponse) SetOutput(v interface{}) {
t := reflect.TypeOf(v)
_, isReader := v.(io.Reader)
if t != nil && t.Kind() != reflect.Chan && !isReader {
v = cmds.Single{Value: v}
}
r.out = v
r.once.Do(func() { close(r.wait) })
}
// Output returns the output variable
func (r *fakeResponse) Output() interface{} {
<-r.wait
return r.out
}
// SetLength forwards the call to the underlying ResponseEmitter
func (r *fakeResponse) SetLength(l uint64) {
r.re.SetLength(l)
}
// Length is an empty stub
func (r *fakeResponse) Length() uint64 {
return 0
}
// Close forwards the call to the underlying ResponseEmitter
func (r *fakeResponse) Close() error {
return r.re.Close()
}
// SetCloser is an empty stub
func (r *fakeResponse) SetCloser(io.Closer) {}
// Reader is an empty stub
func (r *fakeResponse) Reader() (io.Reader, error) {
return nil, nil
}
// Marshal is an empty stub
func (r *fakeResponse) Marshal() (io.Reader, error) {
return nil, nil
}
// Stdout returns os.Stdout
func (r *fakeResponse) Stdout() io.Writer {
return os.Stdout
}
// Stderr returns os.Stderr
func (r *fakeResponse) Stderr() io.Writer {
return os.Stderr
}