mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +08:00
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>
This commit is contained in:
@ -3,6 +3,7 @@ package commands
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
cmds "github.com/ipfs/go-ipfs/commands"
|
cmds "github.com/ipfs/go-ipfs/commands"
|
||||||
"github.com/ipfs/go-ipfs/core"
|
"github.com/ipfs/go-ipfs/core"
|
||||||
@ -61,29 +62,27 @@ The output is:
|
|||||||
res.SetOutput(out)
|
res.SetOutput(out)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PostRun: func(req cmds.Request, res cmds.Response) {
|
Marshalers: cmds.MarshalerMap{
|
||||||
if res.Error() != nil {
|
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||||
return
|
outChan, ok := res.Output().(<-chan interface{})
|
||||||
}
|
if !ok {
|
||||||
outChan, ok := res.Output().(<-chan interface{})
|
return nil, u.ErrCast()
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
errors := false
|
||||||
if errors {
|
for r0 := range outChan {
|
||||||
res.SetError(fmt.Errorf("errors while displaying some entries"), cmds.ErrNormal)
|
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{},
|
Type: filestore.ListRes{},
|
||||||
}
|
}
|
||||||
@ -137,23 +136,22 @@ For ERROR entries the error will also be printed to stderr.
|
|||||||
res.SetOutput(out)
|
res.SetOutput(out)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PostRun: func(req cmds.Request, res cmds.Response) {
|
Marshalers: cmds.MarshalerMap{
|
||||||
if res.Error() != nil {
|
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
||||||
return
|
outChan, ok := res.Output().(<-chan interface{})
|
||||||
}
|
if !ok {
|
||||||
outChan, ok := res.Output().(<-chan interface{})
|
return nil, u.ErrCast()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
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{},
|
Type: filestore.ListRes{},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user