From c65b01c55cd770177a7377e11cb2406445f7f0c5 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow <brian.holderchow@gmail.com> Date: Fri, 7 Nov 2014 05:47:04 -0800 Subject: [PATCH] fix(add) cast safely --- cmd/ipfs2/daemon.go | 2 +- core/commands2/add.go | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/ipfs2/daemon.go b/cmd/ipfs2/daemon.go index 9ffe96d9d..da3603cad 100644 --- a/cmd/ipfs2/daemon.go +++ b/cmd/ipfs2/daemon.go @@ -9,7 +9,7 @@ import ( cmds "github.com/jbenet/go-ipfs/commands" 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" daemon "github.com/jbenet/go-ipfs/daemon2" ) diff --git a/core/commands2/add.go b/core/commands2/add.go index ccb5e86c1..ab22bf3c7 100644 --- a/core/commands2/add.go +++ b/core/commands2/add.go @@ -62,14 +62,18 @@ var addCmd = &cmds.Command{ }, Marshallers: map[cmds.EncodingType]cmds.Marshaller{ cmds.Text: func(res cmds.Response) ([]byte, error) { - v := res.Output().(*AddOutput).Added - if len(v) == 1 { - s := fmt.Sprintf("Added object: %s\n", v[0].Hash) + val, ok := res.Output().(*AddOutput) + if !ok { + 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 } - s := fmt.Sprintf("Added %v objects:\n", len(v)) - for _, obj := range v { + s := fmt.Sprintf("Added %v objects:\n", len(added)) + for _, obj := range added { s += fmt.Sprintf("- %s\n", obj.Hash) } return []byte(s), nil -- GitLab