@cryptotaxi247 / kubo / commits / 7bd7ed6d5

commands: Added output stream field to Response

Matt Bell committed Oct 20, 2014 at 14:38 UTC 7bd7ed6d52ed694f265a7dfcc3d0db504141243c
2 files changed +16 -5
commands/command.go
+5 -3
@@ -4,6 +4,7 @@ import (
4 "errors"
5 "fmt"
6 "strings"
7 + "io"
8
9 u "github.com/jbenet/go-ipfs/util"
10 )
@@ -46,9 +47,10 @@ func (c *Command) Register(id string, sub *Command) error {
47 return nil
48 }
49
49 -// Call invokes the command at the given subcommand path
50 -func (c *Command) Call(req Request) Response {
51 - res := NewResponse(req)
50 +// Call invokes the command for the given Request
51 +// Streaming output is written to `out`
52 +func (c *Command) Call(req Request, out io.Writer) Response {
53 + res := NewResponse(req, out)
54
55 cmds, err := c.Resolve(req.Path())
56 if err != nil {
commands/response.go
+11 -2
@@ -5,6 +5,7 @@ import (
5 "encoding/xml"
6 "fmt"
7 "strings"
8 + "io"
9 )
10
11 // ErrorType signfies a category of errors
@@ -59,6 +60,9 @@ type Response interface {
60 SetValue(interface{})
61 Value() interface{}
62
63 + // Returns the output stream Writer
64 + Stream() io.Writer
65 +
66 // Marshal marshals out the response into a buffer. It uses the EncodingType
67 // on the Request to chose a Marshaller (Codec).
68 Marshal() ([]byte, error)
@@ -68,6 +72,7 @@ type response struct {
72 req Request
73 err *Error
74 value interface{}
75 + out io.Writer
76 }
77
78 func (r *response) Request() Request {
@@ -82,6 +87,10 @@ func (r *response) SetValue(v interface{}) {
87 r.value = v
88 }
89
90 +func (r *response) Stream() io.Writer {
91 + return r.out
92 +}
93 +
94 func (r *response) Error() error {
95 if r.err == nil {
96 return nil
@@ -116,6 +125,6 @@ func (r *response) Marshal() ([]byte, error) {
125 }
126
127 // NewResponse returns a response to match given Request
119 -func NewResponse(req Request) Response {
120 - return &response{req: req}
128 +func NewResponse(req Request, out io.Writer) Response {
129 + return &response{req: req, out: out}
130 }