Skip to content

Commit

Permalink
feat(peers): fix limit/offset bugs, add sending output to less when…
Browse files Browse the repository at this point in the history
… listing peer cache

While working on offset and limit for `qri peers list`, I realized that paginating through the list of peers doesn't really make sense, because to generate the peers list we range through a map, not a slice, we get a different order each time.

We can talk about whether we want to make this list ordered, but for now, I thought it might be a good place to try to stdout to `less`, rather then return the output to the terminal.

So, if you look for your cached peers, qri will send you to `less` that displays your cached peers.
  • Loading branch information
ramfox committed Apr 25, 2019
1 parent f7b042d commit 2e623db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
6 changes: 5 additions & 1 deletion actions/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ func ListPeers(node *p2p.QriNode, limit, offset int, onlineOnly bool) ([]*config
return nil, fmt.Errorf("error listing peers: %s", err.Error())
}

if len(ps) == 0 {
if len(ps) == 0 || offset >= len(ps) {
return []*config.ProfilePod{}, nil
}

for _, pro := range ps {
if offset > 0 {
offset--
continue
}
if len(peers) >= limit {
break
}
Expand Down
26 changes: 24 additions & 2 deletions cmd/peers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"os/exec"

"github.com/ghodss/yaml"
"github.com/qri-io/ioes"
Expand Down Expand Up @@ -218,9 +220,8 @@ func (o *PeersOptions) List() (err error) {
printSuccess(o.Out, "%d.\t%s", i+1, p)
}
} else {

// if we don't have an RPC client, assume we're not connected
if o.UsingRPC && !o.Cached {
if !o.UsingRPC && !o.Cached {
printInfo(o.Out, "qri not connected, listing cached peers")
o.Cached = true
}
Expand All @@ -235,10 +236,31 @@ func (o *PeersOptions) List() (err error) {
return err
}

if o.Cached {
// if cached, print to less
// because the list of peers is non deterministic
// it make sense to try out printing to less here
// where limit and offset don't mean as much
buf := bytes.Buffer{}
fmt.Fprintln(&buf, "Cached Qri Peers List:\n")
for i, peer := range res {
printPeerInfoNoColor(&buf, i, peer)
}
less := exec.Command("less")
less.Stdin = &buf
less.Stdout = o.Out

if err := less.Run(); err != nil {
return err
}
return nil
}

fmt.Fprintln(o.Out, "")
for i, peer := range res {
printPeerInfo(o.Out, i, peer)
}

}
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ func printPeerInfo(w io.Writer, i int, p *config.ProfilePod) {
fmt.Fprintln(w, "")
}

func printPeerInfoNoColor(w io.Writer, i int, p *config.ProfilePod) {
if p.Online {
fmt.Fprintf(w, "%s | %s\n", p.Peername, "online")
} else {
fmt.Fprintf(w, "%s\n", p.Peername)
}
fmt.Fprintf(w, "profile ID: %s\n", p.ID)
if len(p.NetworkAddrs) > 0 {
fmt.Fprintf(w, "address: %s\n", p.NetworkAddrs[0])
}
fmt.Fprintln(w, "")
}

func printResults(w io.Writer, r *dataset.Structure, data []byte, format dataset.DataFormat) {
switch format {
case dataset.JSONDataFormat:
Expand Down

0 comments on commit 2e623db

Please sign in to comment.