-
Notifications
You must be signed in to change notification settings - Fork 384
/
peers.go
67 lines (57 loc) · 1.69 KB
/
peers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"flag"
"fmt"
"time"
ff "github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"moul.io/godev"
"berty.tech/berty/v2/go/pkg/bertytypes"
"berty.tech/berty/v2/go/pkg/errcode"
)
func peersCommand() *ffcli.Command {
var refreshEveryFlag time.Duration = time.Second
fsBuilder := func() (*flag.FlagSet, error) {
fs := flag.NewFlagSet("peers", flag.ExitOnError)
manager.SetupLoggingFlags(fs) // also available at root level
manager.SetupLocalProtocolServerFlags(fs) // by default, start a new local messenger server,
manager.SetupRemoteNodeFlags(fs) // but allow to set a remote server instead
fs.DurationVar(&refreshEveryFlag, "peers.refresh", refreshEveryFlag, "refresh every DURATION (0: no refresh)")
return fs, nil
}
return &ffcli.Command{
Name: "peers",
ShortUsage: "berty [global flags] peers [flags]",
ShortHelp: "list peers",
FlagSetBuilder: fsBuilder,
Options: []ff.Option{ff.WithEnvVarPrefix("BERTY")},
UsageFunc: usageFunc,
Exec: func(ctx context.Context, args []string) error {
if len(args) > 0 {
return flag.ErrHelp
}
// messenger client
protocol, err := manager.GetProtocolClient()
if err != nil {
return err
}
for {
ret, err := protocol.PeerList(ctx, &bertytypes.PeerList_Request{})
if err != nil {
return errcode.TODO.Wrap(err)
}
if refreshEveryFlag == 0 {
fmt.Println(godev.PrettyJSONPB(ret))
break
}
// clear screen
print("\033[H\033[2J")
// FIXME: implement an ascii-table version of this view
fmt.Println(godev.PrettyJSONPB(ret))
time.Sleep(refreshEveryFlag)
}
return nil
},
}
}