@cryptotaxi247 / kubo / commits / a494f48a9

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

* feat(cmds): ipfs id: add offline option * docs: clarify why 'ipfs id <peer>' in offline mode Co-authored-by: Marcin Rataj <lidel@lidel.org>

Lucas Molas committed Feb 10, 2022 at 16:42 UTC a494f48a9dd0a66bd682651b89e7503b5500ac2a
2 files changed +21 -19
core/commands/id.go
+13 -18
@@ -23,14 +23,7 @@ import (
23 identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
24 )
25
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; if you only want to convert --peerid-base, pass --offline option."
27
28 type IdOutput struct {
29 ID string
@@ -102,19 +95,21 @@ EXAMPLE:
95 return cmds.EmitOnce(res, output)
96 }
97
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 {
100 return errors.New(offlineIdErrorMessage)
101 }
102
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 + }
113 }
114
115 output, err := printPeer(keyEnc, n.Peerstore, id)
test/sharness/t0026-id.sh
+8 -1
@@ -48,10 +48,17 @@ test_expect_success "checking ProtocolVersion" '
48 test_cmp expected-protocol-version actual-protocol-version
49 '
50
51 -test_expect_success "checking ID" '
51 +test_expect_success "checking ID of self" '
52 ipfs config Identity.PeerID > expected-id &&
53 ipfs id -f "<id>\n" > actual-id &&
54 test_cmp expected-id actual-id
55 '
56
57 +test_expect_success "checking and converting ID of a random peer while 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 +
64 test_done