Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmds): ipfs id: support --offline option #8626

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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