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 <k@kevina.org>
Kevin Atkinson committed
Feb 8, 2017 at 21:20 UTC
ffe9f3e5662811efee250bed705be81fa123168c
1 file changed
+36
-38
core/commands/filestore.go
+36
-38
@@ -3,6 +3,7 @@ package commands
3
import (
4
"context"
5
"fmt"
6
+ "io"
7
8
cmds "github.com/ipfs/go-ipfs/commands"
9
"github.com/ipfs/go-ipfs/core"
@@ -61,29 +62,27 @@ The output is:
62
res.SetOutput(out)
63
}
64
},
64
- PostRun: func(req cmds.Request, res cmds.Response) {
65
- if res.Error() != nil {
66
- return
67
- }
68
- outChan, ok := res.Output().(<-chan interface{})
69
- if !ok {
70
- res.SetError(u.ErrCast(), cmds.ErrNormal)
71
- return
72
- }
73
- res.SetOutput(nil)
74
- errors := false
75
- for r0 := range outChan {
76
- r := r0.(*filestore.ListRes)
77
- if r.ErrorMsg != "" {
78
- errors = true
79
- fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg)
80
- } else {
81
- fmt.Fprintf(res.Stdout(), "%s\n", r.FormatLong())
65
+ Marshalers: cmds.MarshalerMap{
66
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
67
+ outChan, ok := res.Output().(<-chan interface{})
68
+ if !ok {
69
+ return nil, u.ErrCast()
70
}
83
- }
84
- if errors {
85
- res.SetError(fmt.Errorf("errors while displaying some entries"), cmds.ErrNormal)
86
- }
71
+ errors := false
72
+ for r0 := range outChan {
73
+ r := r0.(*filestore.ListRes)
74
+ if r.ErrorMsg != "" {
75
+ errors = true
76
+ fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg)
77
+ } else {
78
+ fmt.Fprintf(res.Stdout(), "%s\n", r.FormatLong())
79
+ }
80
+ }
81
+ if errors {
82
+ return nil, fmt.Errorf("errors while displaying some entries")
83
+ }
84
+ return nil, nil
85
+ },
86
},
87
Type: filestore.ListRes{},
88
}
@@ -137,23 +136,22 @@ For ERROR entries the error will also be printed to stderr.
136
res.SetOutput(out)
137
}
138
},
140
- PostRun: func(req cmds.Request, res cmds.Response) {
141
- if res.Error() != nil {
142
- return
143
- }
144
- outChan, ok := res.Output().(<-chan interface{})
145
- if !ok {
146
- res.SetError(u.ErrCast(), cmds.ErrNormal)
147
- return
148
- }
149
- res.SetOutput(nil)
150
- for r0 := range outChan {
151
- r := r0.(*filestore.ListRes)
152
- if r.Status == filestore.StatusOtherError {
153
- fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg)
139
+ Marshalers: cmds.MarshalerMap{
140
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
141
+ outChan, ok := res.Output().(<-chan interface{})
142
+ if !ok {
143
+ return nil, u.ErrCast()
144
}
155
- fmt.Fprintf(res.Stdout(), "%s %s\n", r.Status.Format(), r.FormatLong())
156
- }
145
+ res.SetOutput(nil)
146
+ for r0 := range outChan {
147
+ r := r0.(*filestore.ListRes)
148
+ if r.Status == filestore.StatusOtherError {
149
+ fmt.Fprintf(res.Stderr(), "%s\n", r.ErrorMsg)
150
+ }
151
+ fmt.Fprintf(res.Stdout(), "%s %s\n", r.Status.Format(), r.FormatLong())
152
+ }
153
+ return nil, nil
154
+ },
155
},
156
Type: filestore.ListRes{},
157
}