diff --git a/core/commands/id.go b/core/commands/id.go index 06a70bf35b5..9816f7096b1 100644 --- a/core/commands/id.go +++ b/core/commands/id.go @@ -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 @@ -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) diff --git a/test/sharness/t0026-id.sh b/test/sharness/t0026-id.sh index 8e02fac3711..d30454700e9 100755 --- a/test/sharness/t0026-id.sh +++ b/test/sharness/t0026-id.sh @@ -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 "\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 "\n" --peerid-base base36 --offline QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N > actual-id && + test_cmp expected-id actual-id +' + test_done