mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-28 08:47:42 +08:00
Factor out boiler plate code for PostRun in "ipfs filestore ls".
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
This commit is contained in:
@ -8,6 +8,7 @@ package commands
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -149,3 +150,42 @@ func unwrapOutput(i interface{}) (interface{}, error) {
|
|||||||
|
|
||||||
return <-ch, nil
|
return <-ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type nonFatalError string
|
||||||
|
|
||||||
|
// streamRes is a helper function to stream results, that possibly
|
||||||
|
// contain with non-fatal, the helper function is allowed to panic on
|
||||||
|
// internal errors
|
||||||
|
func streamRes(procVal func(interface{}, io.Writer) nonFatalError) func(cmds.Response, cmds.ResponseEmitter) error {
|
||||||
|
return func(res cmds.Response, re cmds.ResponseEmitter) (err error) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
err = fmt.Errorf("internal error: %v", r)
|
||||||
|
}
|
||||||
|
re.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
var errors bool
|
||||||
|
for {
|
||||||
|
v, err := res.Next()
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
errorMsg := procVal(v, os.Stdout)
|
||||||
|
|
||||||
|
if errorMsg != "" {
|
||||||
|
errors = true
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", errorMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors {
|
||||||
|
return fmt.Errorf("errors while displaying some entries")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
|
|
||||||
oldCmds "github.com/ipfs/go-ipfs/commands"
|
oldCmds "github.com/ipfs/go-ipfs/commands"
|
||||||
lgc "github.com/ipfs/go-ipfs/commands/legacy"
|
lgc "github.com/ipfs/go-ipfs/commands/legacy"
|
||||||
@ -73,36 +72,14 @@ The output is:
|
|||||||
return res.Emit(out)
|
return res.Emit(out)
|
||||||
},
|
},
|
||||||
PostRun: cmds.PostRunMap{
|
PostRun: cmds.PostRunMap{
|
||||||
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
|
cmds.CLI: streamRes(func(v interface{}, out io.Writer) nonFatalError {
|
||||||
var errors bool
|
r := v.(*filestore.ListRes)
|
||||||
for {
|
|
||||||
v, err := res.Next()
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
r, ok := v.(*filestore.ListRes)
|
|
||||||
if !ok {
|
|
||||||
return e.New(e.TypeErr(r, v))
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.ErrorMsg != "" {
|
if r.ErrorMsg != "" {
|
||||||
errors = true
|
return nonFatalError(r.ErrorMsg)
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", r.ErrorMsg)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(os.Stdout, "%s\n", r.FormatLong())
|
|
||||||
}
|
}
|
||||||
}
|
fmt.Fprintf(out, "%s\n", r.FormatLong())
|
||||||
|
return ""
|
||||||
if errors {
|
}),
|
||||||
return fmt.Errorf("errors while displaying some entries")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Type: filestore.ListRes{},
|
Type: filestore.ListRes{},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user