commands: Use a flag to enable streaming channel output

This commit is contained in:
Matt Bell 2014-12-17 19:47:32 -08:00
parent abd390b892
commit 981f793df9
3 changed files with 11 additions and 9 deletions

View File

@ -42,6 +42,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
// override with json to send to server
req.SetOption(cmds.EncShort, cmds.JSON)
// stream channel output
req.SetOption(cmds.ChanOpt, "true")
query, err := getQuery(req)
if err != nil {
return nil, err

View File

@ -84,13 +84,6 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(contentTypeHeader, mime)
}
// if the res output is a channel, set a custom header for it
isChan := false
if _, ok := res.Output().(chan interface{}); ok {
w.Header().Set(channelHeader, "1")
isChan = true
}
// if response contains an error, write an HTTP error status code
if e := res.Error(); e != nil {
if e.Code == cmds.ErrClient {
@ -108,11 +101,14 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if isChan {
// if output is a channel and user requested streaming channels,
// use chunk copier for the output
_, isChan := res.Output().(chan interface{})
streamChans, _, _ := req.Option("stream-channels").Bool()
if isChan && streamChans {
err = copyChunks(w, out)
if err != nil {
log.Error(err)
fmt.Println(err)
}
return
}

View File

@ -158,15 +158,18 @@ const (
EncLong = "encoding"
RecShort = "r"
RecLong = "recursive"
ChanOpt = "stream-channels"
)
// options that are used by this package
var OptionEncodingType = StringOption(EncShort, EncLong, "The encoding type the output should be encoded with (json, xml, or text)")
var OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively")
var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output")
// global options, added to every command
var globalOptions = []Option{
OptionEncodingType,
OptionStreamChannels,
}
// the above array of Options, wrapped in a Command