Skip to content

Commit

Permalink
feat(cmds): ipfs id: support --offline option (#8626)
Browse files Browse the repository at this point in the history
* feat(cmds): ipfs id: add offline option
* docs: clarify why 'ipfs id <peer>' in offline mode

Co-authored-by: Marcin Rataj <[email protected]>
  • Loading branch information
schomatis and lidel authored Feb 10, 2022
1 parent 556c038 commit a494f48
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; if you only want to convert --peerid-base, pass --offline option."

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 and converting ID of a random peer while 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 a494f48

Please sign in to comment.