commands: Use a flag to enable streaming channel output
Matt Bell committed
Dec 17, 2014 at 19:47 UTC
981f793df90f5a1aa1201b94ea885448ccd3f249
3 files changed
+11
-9
commands/http/client.go
+3
@@ -42,6 +42,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
42
// override with json to send to server
43
req.SetOption(cmds.EncShort, cmds.JSON)
44
45
+ // stream channel output
46
+ req.SetOption(cmds.ChanOpt, "true")
47
+
48
query, err := getQuery(req)
49
if err != nil {
50
return nil, err
commands/http/handler.go
+5
-9
@@ -84,13 +84,6 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
84
w.Header().Set(contentTypeHeader, mime)
85
}
86
87
- // if the res output is a channel, set a custom header for it
88
- isChan := false
89
- if _, ok := res.Output().(chan interface{}); ok {
90
- w.Header().Set(channelHeader, "1")
91
- isChan = true
92
- }
93
-
87
// if response contains an error, write an HTTP error status code
88
if e := res.Error(); e != nil {
89
if e.Code == cmds.ErrClient {
@@ -108,11 +101,14 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
101
return
102
}
103
111
- if isChan {
104
+ // if output is a channel and user requested streaming channels,
105
+ // use chunk copier for the output
106
+ _, isChan := res.Output().(chan interface{})
107
+ streamChans, _, _ := req.Option("stream-channels").Bool()
108
+ if isChan && streamChans {
109
err = copyChunks(w, out)
110
if err != nil {
111
log.Error(err)
115
- fmt.Println(err)
112
}
113
return
114
}
commands/option.go
+3
@@ -158,15 +158,18 @@ const (
158
EncLong = "encoding"
159
RecShort = "r"
160
RecLong = "recursive"
161
+ ChanOpt = "stream-channels"
162
)
163
164
// options that are used by this package
165
var OptionEncodingType = StringOption(EncShort, EncLong, "The encoding type the output should be encoded with (json, xml, or text)")
166
var OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively")
167
+var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output")
168
169
// global options, added to every command
170
var globalOptions = []Option{
171
OptionEncodingType,
172
+ OptionStreamChannels,
173
}
174
175
// the above array of Options, wrapped in a Command