commands: Made Std{in|out|err} accessible in Request/Response
Matt Bell committed
Jan 22, 2015 at 23:12 UTC
3fc9bedb0bd7138f1ce1a5f9e9a45ebcc2f4c5ce
4 files changed
+34
-9
commands/request.go
+9
-1
@@ -3,6 +3,8 @@ package commands
3
import (
4
"errors"
5
"fmt"
6
+ "io"
7
+ "os"
8
"reflect"
9
"strconv"
10
@@ -78,6 +80,7 @@ type Request interface {
80
SetContext(Context)
81
Command() *Command
82
Values() map[string]interface{}
83
+ Stdin() io.Reader
84
85
ConvertOptions() error
86
}
@@ -91,6 +94,7 @@ type request struct {
94
ctx Context
95
optionDefs map[string]Option
96
values map[string]interface{}
97
+ stdin io.Reader
98
}
99
100
// Path returns the command path of this request
@@ -214,6 +218,10 @@ func (r *request) Values() map[string]interface{} {
218
return r.values
219
}
220
221
+func (r *request) Stdin() io.Reader {
222
+ return r.stdin
223
+}
224
+
225
func (r *request) ConvertOptions() error {
226
for k, v := range r.options {
227
opt, ok := r.optionDefs[k]
@@ -282,7 +290,7 @@ func NewRequest(path []string, opts optMap, args []string, file files.File, cmd
290
291
ctx := Context{Context: context.TODO()}
292
values := make(map[string]interface{})
285
- req := &request{path, opts, args, file, cmd, ctx, optDefs, values}
293
+ req := &request{path, opts, args, file, cmd, ctx, optDefs, values, os.Stdin}
294
err := req.ConvertOptions()
295
if err != nil {
296
return nil, err
commands/response.go
+20
-1
@@ -6,6 +6,7 @@ import (
6
"encoding/xml"
7
"fmt"
8
"io"
9
+ "os"
10
"strings"
11
)
12
@@ -105,6 +106,10 @@ type Response interface {
106
107
// Gets a io.Reader that reads the marshalled output
108
Reader() (io.Reader, error)
109
+
110
+ // Gets Stdout and Stderr, for writing to console without using SetOutput
111
+ Stdout() io.Writer
112
+ Stderr() io.Writer
113
}
114
115
type response struct {
@@ -113,6 +118,8 @@ type response struct {
118
value interface{}
119
out io.Reader
120
length uint64
121
+ stdout io.Writer
122
+ stderr io.Writer
123
}
124
125
func (r *response) Request() Request {
@@ -206,7 +213,19 @@ func (r *response) Reader() (io.Reader, error) {
213
return r.out, nil
214
}
215
216
+func (r *response) Stdout() io.Writer {
217
+ return r.stdout
218
+}
219
+
220
+func (r *response) Stderr() io.Writer {
221
+ return r.stderr
222
+}
223
+
224
// NewResponse returns a response to match given Request
225
func NewResponse(req Request) Response {
211
- return &response{req: req}
226
+ return &response{
227
+ req: req,
228
+ stdout: os.Stdout,
229
+ stderr: os.Stderr,
230
+ }
231
}
core/commands/add.go
+4
-5
@@ -4,7 +4,6 @@ import (
4
"errors"
5
"fmt"
6
"io"
7
- "os"
7
"path"
8
"strings"
9
@@ -139,7 +138,7 @@ remains to be implemented.
138
bar.Callback = func(line string) {
139
terminalWidth = len(line)
140
bar.Callback = nil
142
- bar.Output = os.Stderr
141
+ bar.Output = res.Stderr()
142
log.Infof("terminal width: %v\n", terminalWidth)
143
}
144
bar.Update()
@@ -153,12 +152,12 @@ remains to be implemented.
152
if len(output.Hash) > 0 {
153
if showProgressBar {
154
// clear progress bar line before we print "added x" output
156
- fmt.Fprintf(os.Stderr, "\r%s\r", strings.Repeat(" ", terminalWidth))
155
+ fmt.Fprintf(res.Stderr(), "\r%s\r", strings.Repeat(" ", terminalWidth))
156
}
157
if quiet {
159
- fmt.Printf("%s\n", output.Hash)
158
+ fmt.Fprintf(res.Stdout(), "%s\n", output.Hash)
159
} else {
161
- fmt.Printf("added %s %s\n", output.Hash, output.Name)
160
+ fmt.Fprintf(res.Stdout(), "added %s %s\n", output.Hash, output.Name)
161
}
162
163
} else {
core/commands/cat.go
+1
-2
@@ -2,7 +2,6 @@ package commands
2
3
import (
4
"io"
5
- "os"
5
6
cmds "github.com/jbenet/go-ipfs/commands"
7
core "github.com/jbenet/go-ipfs/core"
@@ -51,7 +50,7 @@ it contains.
50
}
51
52
bar := pb.New(int(res.Length())).SetUnits(pb.U_BYTES)
54
- bar.Output = os.Stderr
53
+ bar.Output = res.Stderr()
54
bar.Start()
55
56
reader := bar.NewProxyReader(res.Output().(io.Reader))