From 21d2838d4f4141d4cf7d9ee0caba524c9fc55e50 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Mon, 17 Nov 2014 23:46:46 -0800 Subject: [PATCH] cmds/id: fixed args + err checks --- core/commands2/id.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/core/commands2/id.go b/core/commands2/id.go index 3a0bd6a77be..8d9c773392f 100644 --- a/core/commands2/id.go +++ b/core/commands2/id.go @@ -16,8 +16,12 @@ import ( u "github.com/jbenet/go-ipfs/util" ) -const offlineIdErrorMessage = `ID command fails when run without daemon, we are working -to fix this In the meantime, please run the daemon if you want to use 'ipfs id'` +const offlineIdErrorMessage = `ID command fails when run without daemon, we are working to fix this. +In the meantime, please run the daemon if you want to use 'ipfs id': + + ipfs daemon & + ipfs id QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ +` type IdOutput struct { ID string @@ -35,7 +39,9 @@ Prints out information about the specified peer, if no peer is specified, prints out local peers info. `, }, - Arguments: nil, + Arguments: []cmds.Argument{ + cmds.StringArg("peerid", false, false, "peer.ID of node to look up"), + }, Run: func(req cmds.Request) (interface{}, error) { node, err := req.Context().GetNode() if err != nil { @@ -48,11 +54,19 @@ if no peer is specified, prints out local peers info. pid, ok := req.Arguments()[0].(string) if !ok { - return nil, errors.New("Improperly formatted peer id") + return nil, cmds.ClientError("Improperly formatted peer id") } id := peer.ID(b58.Decode(pid)) + if len(id) == 0 { + return nil, cmds.ClientError("Invalid peer id") + } + ctx, _ := context.WithTimeout(context.TODO(), time.Second*5) + if node.Routing == nil { + return nil, errors.New(offlineIdErrorMessage) + } + p, err := node.Routing.FindPeer(ctx, id) if err == kb.ErrLookupFailure { return nil, errors.New(offlineIdErrorMessage)