@cryptotaxi247 / kubo / commits / 21d2838d4

cmds/id: fixed args + err checks

Juan Batiz-Benet committed Nov 17, 2014 at 23:46 UTC 21d2838d4f4141d4cf7d9ee0caba524c9fc55e50
1 file changed +18 -4
core/commands2/id.go
+18 -4
@@ -16,8 +16,12 @@ import (
16 u "github.com/jbenet/go-ipfs/util"
17 )
18
19 -const offlineIdErrorMessage = `ID command fails when run without daemon, we are working
20 -to fix this In the meantime, please run the daemon if you want to use 'ipfs id'`
19 +const offlineIdErrorMessage = `ID command fails when run without daemon, we are working to fix this.
20 +In the meantime, please run the daemon if you want to use 'ipfs id':
21 +
22 + ipfs daemon &
23 + ipfs id QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
24 +`
25
26 type IdOutput struct {
27 ID string
@@ -35,7 +39,9 @@ Prints out information about the specified peer,
39 if no peer is specified, prints out local peers info.
40 `,
41 },
38 - Arguments: nil,
42 + Arguments: []cmds.Argument{
43 + cmds.StringArg("peerid", false, false, "peer.ID of node to look up"),
44 + },
45 Run: func(req cmds.Request) (interface{}, error) {
46 node, err := req.Context().GetNode()
47 if err != nil {
@@ -48,11 +54,19 @@ if no peer is specified, prints out local peers info.
54
55 pid, ok := req.Arguments()[0].(string)
56 if !ok {
51 - return nil, errors.New("Improperly formatted peer id")
57 + return nil, cmds.ClientError("Improperly formatted peer id")
58 }
59
60 id := peer.ID(b58.Decode(pid))
61 + if len(id) == 0 {
62 + return nil, cmds.ClientError("Invalid peer id")
63 + }
64 +
65 ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
66 + if node.Routing == nil {
67 + return nil, errors.New(offlineIdErrorMessage)
68 + }
69 +
70 p, err := node.Routing.FindPeer(ctx, id)
71 if err == kb.ErrLookupFailure {
72 return nil, errors.New(offlineIdErrorMessage)