Commit 3fc9bedb authored by Matt Bell's avatar Matt Bell

commands: Made Std{in|out|err} accessible in Request/Response

parent b77d1c21
...@@ -3,6 +3,8 @@ package commands ...@@ -3,6 +3,8 @@ package commands
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"os"
"reflect" "reflect"
"strconv" "strconv"
...@@ -78,6 +80,7 @@ type Request interface { ...@@ -78,6 +80,7 @@ type Request interface {
SetContext(Context) SetContext(Context)
Command() *Command Command() *Command
Values() map[string]interface{} Values() map[string]interface{}
Stdin() io.Reader
ConvertOptions() error ConvertOptions() error
} }
...@@ -91,6 +94,7 @@ type request struct { ...@@ -91,6 +94,7 @@ type request struct {
ctx Context ctx Context
optionDefs map[string]Option optionDefs map[string]Option
values map[string]interface{} values map[string]interface{}
stdin io.Reader
} }
// Path returns the command path of this request // Path returns the command path of this request
...@@ -214,6 +218,10 @@ func (r *request) Values() map[string]interface{} { ...@@ -214,6 +218,10 @@ func (r *request) Values() map[string]interface{} {
return r.values return r.values
} }
func (r *request) Stdin() io.Reader {
return r.stdin
}
func (r *request) ConvertOptions() error { func (r *request) ConvertOptions() error {
for k, v := range r.options { for k, v := range r.options {
opt, ok := r.optionDefs[k] opt, ok := r.optionDefs[k]
...@@ -282,7 +290,7 @@ func NewRequest(path []string, opts optMap, args []string, file files.File, cmd ...@@ -282,7 +290,7 @@ func NewRequest(path []string, opts optMap, args []string, file files.File, cmd
ctx := Context{Context: context.TODO()} ctx := Context{Context: context.TODO()}
values := make(map[string]interface{}) values := make(map[string]interface{})
req := &request{path, opts, args, file, cmd, ctx, optDefs, values} req := &request{path, opts, args, file, cmd, ctx, optDefs, values, os.Stdin}
err := req.ConvertOptions() err := req.ConvertOptions()
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"os"
"strings" "strings"
) )
...@@ -105,6 +106,10 @@ type Response interface { ...@@ -105,6 +106,10 @@ type Response interface {
// Gets a io.Reader that reads the marshalled output // Gets a io.Reader that reads the marshalled output
Reader() (io.Reader, error) Reader() (io.Reader, error)
// Gets Stdout and Stderr, for writing to console without using SetOutput
Stdout() io.Writer
Stderr() io.Writer
} }
type response struct { type response struct {
...@@ -113,6 +118,8 @@ type response struct { ...@@ -113,6 +118,8 @@ type response struct {
value interface{} value interface{}
out io.Reader out io.Reader
length uint64 length uint64
stdout io.Writer
stderr io.Writer
} }
func (r *response) Request() Request { func (r *response) Request() Request {
...@@ -206,7 +213,19 @@ func (r *response) Reader() (io.Reader, error) { ...@@ -206,7 +213,19 @@ func (r *response) Reader() (io.Reader, error) {
return r.out, nil return r.out, nil
} }
func (r *response) Stdout() io.Writer {
return r.stdout
}
func (r *response) Stderr() io.Writer {
return r.stderr
}
// NewResponse returns a response to match given Request // NewResponse returns a response to match given Request
func NewResponse(req Request) Response { func NewResponse(req Request) Response {
return &response{req: req} return &response{
req: req,
stdout: os.Stdout,
stderr: os.Stderr,
}
} }
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"os"
"path" "path"
"strings" "strings"
...@@ -139,7 +138,7 @@ remains to be implemented. ...@@ -139,7 +138,7 @@ remains to be implemented.
bar.Callback = func(line string) { bar.Callback = func(line string) {
terminalWidth = len(line) terminalWidth = len(line)
bar.Callback = nil bar.Callback = nil
bar.Output = os.Stderr bar.Output = res.Stderr()
log.Infof("terminal width: %v\n", terminalWidth) log.Infof("terminal width: %v\n", terminalWidth)
} }
bar.Update() bar.Update()
...@@ -153,12 +152,12 @@ remains to be implemented. ...@@ -153,12 +152,12 @@ remains to be implemented.
if len(output.Hash) > 0 { if len(output.Hash) > 0 {
if showProgressBar { if showProgressBar {
// clear progress bar line before we print "added x" output // clear progress bar line before we print "added x" output
fmt.Fprintf(os.Stderr, "\r%s\r", strings.Repeat(" ", terminalWidth)) fmt.Fprintf(res.Stderr(), "\r%s\r", strings.Repeat(" ", terminalWidth))
} }
if quiet { if quiet {
fmt.Printf("%s\n", output.Hash) fmt.Fprintf(res.Stdout(), "%s\n", output.Hash)
} else { } else {
fmt.Printf("added %s %s\n", output.Hash, output.Name) fmt.Fprintf(res.Stdout(), "added %s %s\n", output.Hash, output.Name)
} }
} else { } else {
......
...@@ -2,7 +2,6 @@ package commands ...@@ -2,7 +2,6 @@ package commands
import ( import (
"io" "io"
"os"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
core "github.com/jbenet/go-ipfs/core" core "github.com/jbenet/go-ipfs/core"
...@@ -51,7 +50,7 @@ it contains. ...@@ -51,7 +50,7 @@ it contains.
} }
bar := pb.New(int(res.Length())).SetUnits(pb.U_BYTES) bar := pb.New(int(res.Length())).SetUnits(pb.U_BYTES)
bar.Output = os.Stderr bar.Output = res.Stderr()
bar.Start() bar.Start()
reader := bar.NewProxyReader(res.Output().(io.Reader)) reader := bar.NewProxyReader(res.Output().(io.Reader))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment