Skip to content

Commit

Permalink
cmds/id: fixed args + err checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenet committed Nov 18, 2014
1 parent f47b4f1 commit 21d2838
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions core/commands2/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 21d2838

Please sign in to comment.