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