forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponseemitter.go
198 lines (163 loc) · 3.66 KB
/
responseemitter.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
package cli
import (
"context"
"fmt"
"io"
"os"
"sync"
cmds "github.com/ipfs/go-ipfs-cmds"
)
var _ ResponseEmitter = &responseEmitter{}
// NewResponseEmitter constructs a new response emitter that writes results to
// the console.
func NewResponseEmitter(stdout, stderr io.Writer, req *cmds.Request) (ResponseEmitter, error) {
encType, enc, err := cmds.GetEncoder(req, stdout, cmds.TextNewline)
return &responseEmitter{
stdout: stdout,
stderr: stderr,
encType: encType,
enc: enc,
}, err
}
// ResponseEmitter extends cmds.ResponseEmitter to give better control over the command line
type ResponseEmitter interface {
cmds.ResponseEmitter
Stdout() io.Writer
Stderr() io.Writer
// SetStatus sets the exit status for this command.
SetStatus(int)
// Status returns the exit status for the command.
Status() int
}
type responseEmitter struct {
l sync.Mutex
stdout io.Writer
stderr io.Writer
length uint64
enc cmds.Encoder
encType cmds.EncodingType
exit int
closed bool
}
func (re *responseEmitter) Type() cmds.PostRunType {
return cmds.CLI
}
func (re *responseEmitter) SetLength(l uint64) {
re.length = l
}
func (re *responseEmitter) isClosed() bool {
re.l.Lock()
defer re.l.Unlock()
return re.closed
}
func (re *responseEmitter) Close() error {
return re.CloseWithError(nil)
}
func (re *responseEmitter) CloseWithError(err error) error {
re.l.Lock()
defer re.l.Unlock()
if re.closed {
return cmds.ErrClosingClosedEmitter
}
re.closed = true
var msg string
if err != nil {
if re.exit == 0 {
// Default "error" exit code.
re.exit = 1
}
switch err {
case context.Canceled:
msg = "canceled"
case context.DeadlineExceeded:
msg = "timed out"
default:
msg = err.Error()
}
fmt.Fprintln(re.stderr, "Error:", msg)
}
defer func() {
re.stdout = nil
re.stderr = nil
}()
var errStderr, errStdout error
if f, ok := re.stderr.(*os.File); ok {
errStderr = f.Sync()
}
if f, ok := re.stdout.(*os.File); ok {
errStdout = f.Sync()
}
// ignore error if the operating system doesn't support syncing std{out,err}
if errStderr != nil && !isSyncNotSupportedErr(errStderr) {
return errStderr
}
if errStdout != nil && !isSyncNotSupportedErr(errStdout) {
return errStdout
}
return nil
}
func (re *responseEmitter) Emit(v interface{}) error {
var isSingle bool
// unwrap
if val, ok := v.(cmds.Single); ok {
v = val.Value
isSingle = true
}
// channel emission iteration
if ch, ok := v.(chan interface{}); ok {
v = (<-chan interface{})(ch)
}
if ch, isChan := v.(<-chan interface{}); isChan {
return cmds.EmitChan(re, ch)
}
// TODO find a better solution for this.
// Idea: use the actual cmd.Type and not *cmd.Type
// would need to fix all commands though
switch c := v.(type) {
case *string:
v = *c
case *int:
v = *c
}
if re.isClosed() {
return cmds.ErrClosedEmitter
}
var err error
switch t := v.(type) {
case io.Reader:
_, err = io.Copy(re.stdout, t)
if err != nil {
return err
}
default:
if re.enc != nil {
err = re.enc.Encode(v)
} else {
_, err = fmt.Fprintln(re.stdout, t)
}
}
if isSingle {
return re.CloseWithError(err)
}
return err
}
// Stderr returns the ResponseWriter's stderr
func (re *responseEmitter) Stderr() io.Writer {
return re.stderr
}
// Stdout returns the ResponseWriter's stdout
func (re *responseEmitter) Stdout() io.Writer {
return re.stdout
}
// SetStatus sets the exit status of the command.
func (re *responseEmitter) SetStatus(code int) {
re.l.Lock()
defer re.l.Unlock()
re.exit = code
}
// Status _returns_ the exit status of the command.
func (re *responseEmitter) Status() int {
re.l.Lock()
defer re.l.Unlock()
return re.exit
}