1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 19:24:14 +08:00

fix(add) cast safely

This commit is contained in:
Brian Tiger Chow
2014-11-07 05:47:04 -08:00
committed by Juan Batiz-Benet
parent 084ffd97aa
commit c65b01c55c
2 changed files with 10 additions and 6 deletions

View File

@ -9,7 +9,7 @@ import (
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
cmdsHttp "github.com/jbenet/go-ipfs/commands/http" cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
"github.com/jbenet/go-ipfs/core" core "github.com/jbenet/go-ipfs/core"
commands "github.com/jbenet/go-ipfs/core/commands2" commands "github.com/jbenet/go-ipfs/core/commands2"
daemon "github.com/jbenet/go-ipfs/daemon2" daemon "github.com/jbenet/go-ipfs/daemon2"
) )

View File

@ -62,14 +62,18 @@ var addCmd = &cmds.Command{
}, },
Marshallers: map[cmds.EncodingType]cmds.Marshaller{ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(res cmds.Response) ([]byte, error) { cmds.Text: func(res cmds.Response) ([]byte, error) {
v := res.Output().(*AddOutput).Added val, ok := res.Output().(*AddOutput)
if len(v) == 1 { if !ok {
s := fmt.Sprintf("Added object: %s\n", v[0].Hash) return nil, errors.New("cast err")
}
added := val.Added
if len(added) == 1 {
s := fmt.Sprintf("Added object: %s\n", added[0].Hash)
return []byte(s), nil return []byte(s), nil
} }
s := fmt.Sprintf("Added %v objects:\n", len(v)) s := fmt.Sprintf("Added %v objects:\n", len(added))
for _, obj := range v { for _, obj := range added {
s += fmt.Sprintf("- %s\n", obj.Hash) s += fmt.Sprintf("- %s\n", obj.Hash)
} }
return []byte(s), nil return []byte(s), nil