Skip to content

Commit 11d7450

Browse files
committed
feat(cmds): ipfs id: add offline option
1 parent 14046d0 commit 11d7450

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

core/commands/id.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ import (
2323
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
2424
)
2525

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

3528
type IdOutput struct {
3629
ID string
@@ -102,19 +95,21 @@ EXAMPLE:
10295
return cmds.EmitOnce(res, output)
10396
}
10497

105-
// TODO handle offline mode with polymorphism instead of conditionals
106-
if !n.IsOnline {
98+
offline, _ := req.Options[OfflineOption].(bool)
99+
if !offline && !n.IsOnline {
107100
return errors.New(offlineIdErrorMessage)
108101
}
109102

110-
// We need to actually connect to run identify.
111-
err = n.PeerHost.Connect(req.Context, peer.AddrInfo{ID: id})
112-
switch err {
113-
case nil:
114-
case kb.ErrLookupFailure:
115-
return errors.New(offlineIdErrorMessage)
116-
default:
117-
return err
103+
if !offline {
104+
// We need to actually connect to run identify.
105+
err = n.PeerHost.Connect(req.Context, peer.AddrInfo{ID: id})
106+
switch err {
107+
case nil:
108+
case kb.ErrLookupFailure:
109+
return errors.New(offlineIdErrorMessage)
110+
default:
111+
return err
112+
}
118113
}
119114

120115
output, err := printPeer(keyEnc, n.Peerstore, id)

test/sharness/t0026-id.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@ test_expect_success "checking ProtocolVersion" '
4848
test_cmp expected-protocol-version actual-protocol-version
4949
'
5050

51-
test_expect_success "checking ID" '
51+
test_expect_success "checking ID of self" '
5252
ipfs config Identity.PeerID > expected-id &&
5353
ipfs id -f "<id>\n" > actual-id &&
5454
test_cmp expected-id actual-id
5555
'
5656

57+
test_expect_success "checking ID of random peer offline" '
58+
# Peer ID taken from `t0140-swarm.sh` test.
59+
echo k2k4r8ncs1yoluq95unsd7x2vfhgve0ncjoggwqx9vyh3vl8warrcp15 > expected-id &&
60+
ipfs id -f "<id>\n" --peerid-base base36 --offline QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N > actual-id &&
61+
test_cmp expected-id actual-id
62+
'
63+
5764
test_done

0 commit comments

Comments
 (0)