提交 981f793d 作者: Matt Bell

commands: Use a flag to enable streaming channel output

上级 abd390b8
...@@ -42,6 +42,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) { ...@@ -42,6 +42,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
// override with json to send to server // override with json to send to server
req.SetOption(cmds.EncShort, cmds.JSON) req.SetOption(cmds.EncShort, cmds.JSON)
// stream channel output
req.SetOption(cmds.ChanOpt, "true")
query, err := getQuery(req) query, err := getQuery(req)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -84,13 +84,6 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -84,13 +84,6 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(contentTypeHeader, mime) 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 response contains an error, write an HTTP error status code
if e := res.Error(); e != nil { if e := res.Error(); e != nil {
if e.Code == cmds.ErrClient { if e.Code == cmds.ErrClient {
...@@ -108,11 +101,14 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -108,11 +101,14 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return 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) err = copyChunks(w, out)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
fmt.Println(err)
} }
return return
} }
......
...@@ -158,15 +158,18 @@ const ( ...@@ -158,15 +158,18 @@ const (
EncLong = "encoding" EncLong = "encoding"
RecShort = "r" RecShort = "r"
RecLong = "recursive" RecLong = "recursive"
ChanOpt = "stream-channels"
) )
// options that are used by this package // 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 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 OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively")
var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output")
// global options, added to every command // global options, added to every command
var globalOptions = []Option{ var globalOptions = []Option{
OptionEncodingType, OptionEncodingType,
OptionStreamChannels,
} }
// the above array of Options, wrapped in a Command // the above array of Options, wrapped in a Command
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论