From ffe9f3e5662811efee250bed705be81fa123168c Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Wed, 8 Feb 2017 21:20:32 -0500 Subject: [PATCH] filestore util: Use a Marshaler instead of PostRun... and just output directly to Stderr and Stdout instead of returning a reader. License: MIT Signed-off-by: Kevin Atkinson --- core/commands/filestore.go | 74 +++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/core/commands/filestore.go b/core/commands/filestore.go index 9ce103b50..6ab35f3f0 100644 --- a/core/commands/filestore.go +++ b/core/commands/filestore.go @@ -3,6 +3,7 @@ package commands import ( "context" "fmt" + "io" cmds "github.com/ipfs/go-ipfs/commands" "github.com/ipfs/go-ipfs/core" @@ -61,29 +62,27 @@ The output is: res.SetOutput(out) } }, - PostRun: func(req cmds.Request, res cmds.Response) { - if res.Error() != nil { - return - } - outChan, ok := res.Output().(<-chan interface{}) - if !ok { - res.SetError(u.ErrCast(), cmds.ErrNormal) - return - } - res.SetOutput(nil) - errors := false - for r0 := range outChan { - r := r0.(*filestore.ListRes) - if r.ErrorMsg != "" { - errors = true - fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg) - } else { - fmt.Fprintf(res.Stdout(), "%s\n", r.FormatLong()) + Marshalers: cmds.MarshalerMap{ + cmds.Text: func(res cmds.Response) (io.Reader, error) { + outChan, ok := res.Output().(<-chan interface{}) + if !ok { + return nil, u.ErrCast() } - } - if errors { - res.SetError(fmt.Errorf("errors while displaying some entries"), cmds.ErrNormal) - } + errors := false + for r0 := range outChan { + r := r0.(*filestore.ListRes) + if r.ErrorMsg != "" { + errors = true + fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg) + } else { + fmt.Fprintf(res.Stdout(), "%s\n", r.FormatLong()) + } + } + if errors { + return nil, fmt.Errorf("errors while displaying some entries") + } + return nil, nil + }, }, Type: filestore.ListRes{}, } @@ -137,23 +136,22 @@ For ERROR entries the error will also be printed to stderr. res.SetOutput(out) } }, - PostRun: func(req cmds.Request, res cmds.Response) { - if res.Error() != nil { - return - } - outChan, ok := res.Output().(<-chan interface{}) - if !ok { - res.SetError(u.ErrCast(), cmds.ErrNormal) - return - } - res.SetOutput(nil) - for r0 := range outChan { - r := r0.(*filestore.ListRes) - if r.Status == filestore.StatusOtherError { - fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg) + Marshalers: cmds.MarshalerMap{ + cmds.Text: func(res cmds.Response) (io.Reader, error) { + outChan, ok := res.Output().(<-chan interface{}) + if !ok { + return nil, u.ErrCast() } - fmt.Fprintf(res.Stdout(), "%s %s\n", r.Status.Format(), r.FormatLong()) - } + res.SetOutput(nil) + for r0 := range outChan { + r := r0.(*filestore.ListRes) + if r.Status == filestore.StatusOtherError { + fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg) + } + fmt.Fprintf(res.Stdout(), "%s %s\n", r.Status.Format(), r.FormatLong()) + } + return nil, nil + }, }, Type: filestore.ListRes{}, }