Skip to content

Commit

Permalink
feat(cmds): ipfs id: add offline option
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis committed Jan 14, 2022
1 parent 14046d0 commit 11d7450
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
31 changes: 13 additions & 18 deletions core/commands/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ import (
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
)

const offlineIdErrorMessage = `'ipfs id' currently cannot query information on remote
peers without a running daemon; we are working to fix this.
In the meantime, if you want to query remote peers using 'ipfs id',
please run the daemon:
ipfs daemon &
ipfs id QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
`
const offlineIdErrorMessage = "'ipfs id' cannot query information on remote peers without a running daemon; use the --offline option instead."

type IdOutput struct {
ID string
Expand Down Expand Up @@ -102,19 +95,21 @@ EXAMPLE:
return cmds.EmitOnce(res, output)
}

// TODO handle offline mode with polymorphism instead of conditionals
if !n.IsOnline {
offline, _ := req.Options[OfflineOption].(bool)
if !offline && !n.IsOnline {
return errors.New(offlineIdErrorMessage)
}

// We need to actually connect to run identify.
err = n.PeerHost.Connect(req.Context, peer.AddrInfo{ID: id})
switch err {
case nil:
case kb.ErrLookupFailure:
return errors.New(offlineIdErrorMessage)
default:
return err
if !offline {
// We need to actually connect to run identify.
err = n.PeerHost.Connect(req.Context, peer.AddrInfo{ID: id})
switch err {
case nil:
case kb.ErrLookupFailure:
return errors.New(offlineIdErrorMessage)
default:
return err
}
}

output, err := printPeer(keyEnc, n.Peerstore, id)
Expand Down
9 changes: 8 additions & 1 deletion test/sharness/t0026-id.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ test_expect_success "checking ProtocolVersion" '
test_cmp expected-protocol-version actual-protocol-version
'

test_expect_success "checking ID" '
test_expect_success "checking ID of self" '
ipfs config Identity.PeerID > expected-id &&
ipfs id -f "<id>\n" > actual-id &&
test_cmp expected-id actual-id
'

test_expect_success "checking ID of random peer offline" '
# Peer ID taken from `t0140-swarm.sh` test.
echo k2k4r8ncs1yoluq95unsd7x2vfhgve0ncjoggwqx9vyh3vl8warrcp15 > expected-id &&
ipfs id -f "<id>\n" --peerid-base base36 --offline QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N > actual-id &&
test_cmp expected-id actual-id
'

test_done

0 comments on commit 11d7450

Please sign in to comment.