Skip to content

Commit 1d59331

Browse files
Merge pull request #3820 from ipfs/kevina/filestore-util-fix
filestore util: Use a Marshaler instead of PostRun...
2 parents c5bb2e9 + ffe9f3e commit 1d59331

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

core/commands/filestore.go

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"context"
55
"fmt"
6+
"io"
67

78
cmds "github.com/ipfs/go-ipfs/commands"
89
"github.com/ipfs/go-ipfs/core"
@@ -61,29 +62,27 @@ The output is:
6162
res.SetOutput(out)
6263
}
6364
},
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()
8270
}
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+
},
8786
},
8887
Type: filestore.ListRes{},
8988
}
@@ -137,23 +136,22 @@ For ERROR entries the error will also be printed to stderr.
137136
res.SetOutput(out)
138137
}
139138
},
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()
154144
}
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+
},
157155
},
158156
Type: filestore.ListRes{},
159157
}

0 commit comments

Comments
 (0)