Skip to content

Commit

Permalink
filestore util: Use a Marshaler instead of PostRun...
Browse files Browse the repository at this point in the history
and just output directly to Stderr and Stdout instead of returning
a reader.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Mar 23, 2017
1 parent 68396b1 commit ab1d575
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"context"
"fmt"
"io"

bs "github.com/ipfs/go-ipfs/blocks/blockstore"
butil "github.com/ipfs/go-ipfs/blocks/blockstore/util"
Expand Down Expand Up @@ -64,29 +65,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{},
}
Expand Down Expand Up @@ -140,23 +139,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{},
}
Expand Down Expand Up @@ -239,8 +237,8 @@ Remove blocks from either the filestore or the main blockstore.
}
ch, err := filestore.RmBlocks(fs, n.Blockstore, n.Pinning, cids, butil.RmBlocksOpts{
Prefix: prefix,
Quiet: quiet,
Force: force,
Quiet: quiet,
Force: force,
})
if err != nil {
res.SetError(err, cmds.ErrNormal)
Expand All @@ -249,7 +247,7 @@ Remove blocks from either the filestore or the main blockstore.
res.SetOutput(ch)
},
PostRun: blockRmCmd.PostRun,
Type: butil.RemovedBlock{},
Type: butil.RemovedBlock{},
}
func getFilestore(req cmds.Request) (*core.IpfsNode, *filestore.Filestore, error) {
n, err := req.InvocContext().GetNode()
Expand Down

0 comments on commit ab1d575

Please sign in to comment.