Skip to content

Commit

Permalink
Merge pull request #7409 from ipfs/feat/id-list-protocols
Browse files Browse the repository at this point in the history
feat: return supported protocols in id output
  • Loading branch information
aschmahmann committed Aug 14, 2020
2 parents 97fee07 + 34c0e5c commit dc869c9
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions core/commands/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"sort"
"strings"

version "github.com/ipfs/go-ipfs"
Expand Down Expand Up @@ -36,6 +37,7 @@ type IdOutput struct {
Addresses []string
AgentVersion string
ProtocolVersion string
Protocols []string
}

const (
Expand Down Expand Up @@ -97,15 +99,17 @@ EXAMPLE:
return errors.New(offlineIdErrorMessage)
}

p, err := n.Routing.FindPeer(req.Context, id)
if err == kb.ErrLookupFailure {
// 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)
}
if err != nil {
default:
return err
}

output, err := printPeer(n.Peerstore, p.ID)
output, err := printPeer(n.Peerstore, id)
if err != nil {
return err
}
Expand All @@ -121,6 +125,7 @@ EXAMPLE:
output = strings.Replace(output, "<pver>", out.ProtocolVersion, -1)
output = strings.Replace(output, "<pubkey>", out.PublicKey, -1)
output = strings.Replace(output, "<addrs>", strings.Join(out.Addresses, "\n"), -1)
output = strings.Replace(output, "<protocols>", strings.Join(out.Protocols, "\n"), -1)
output = strings.Replace(output, "\\n", "\n", -1)
output = strings.Replace(output, "\\t", "\t", -1)
fmt.Fprint(w, output)
Expand Down Expand Up @@ -163,6 +168,13 @@ func printPeer(ps pstore.Peerstore, p peer.ID) (interface{}, error) {
for _, a := range addrs {
info.Addresses = append(info.Addresses, a.String())
}
sort.Strings(info.Addresses)

protocols, _ := ps.GetProtocols(p) // don't care about errors here.
for _, p := range protocols {
info.Protocols = append(info.Protocols, string(p))
}
sort.Strings(info.Protocols)

if v, err := ps.Get(p, "ProtocolVersion"); err == nil {
if vs, ok := v.(string); ok {
Expand Down Expand Up @@ -198,6 +210,9 @@ func printSelf(node *core.IpfsNode) (interface{}, error) {
for _, a := range addrs {
info.Addresses = append(info.Addresses, a.String())
}
sort.Strings(info.Addresses)
info.Protocols = node.PeerHost.Mux().Protocols()
sort.Strings(info.Protocols)
}
info.ProtocolVersion = identify.LibP2PVersion
info.AgentVersion = version.UserAgent
Expand Down

0 comments on commit dc869c9

Please sign in to comment.