-
Notifications
You must be signed in to change notification settings - Fork 384
/
systeminfo.go
91 lines (78 loc) · 2.31 KB
/
systeminfo.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"flag"
"fmt"
"time"
"github.com/peterbourgon/ff/v3/ffcli"
"moul.io/godev"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/messengertypes"
)
func systemInfoCommand() *ffcli.Command {
var (
refreshEveryFlag time.Duration
anonymizeFlag bool
)
fsBuilder := func() (*flag.FlagSet, error) {
fs := flag.NewFlagSet("info", flag.ExitOnError)
fs.String("config", "", "config file (optional)")
manager.SetupLoggingFlags(fs) // also available at root level
manager.SetupLocalMessengerServerFlags(fs) // by default, start a new local messenger server,
manager.SetupRemoteNodeFlags(fs) // but allow to set a remote server instead
fs.DurationVar(&refreshEveryFlag, "info.refresh", refreshEveryFlag, "refresh every DURATION (0: no refresh)")
fs.BoolVar(&anonymizeFlag, "info.anonymize", false, "anonymize output for sharing")
return fs, nil
}
return &ffcli.Command{
Name: "info",
ShortUsage: "berty [global flags] info [flags]",
ShortHelp: "display system info",
FlagSetBuilder: fsBuilder,
Options: ffSubcommandOptions(),
UsageFunc: usageFunc,
Exec: func(ctx context.Context, args []string) error {
if len(args) > 0 {
return flag.ErrHelp
}
// messenger client
messenger, err := manager.GetMessengerClient()
if err != nil {
return err
}
for {
ret, err := messenger.SystemInfo(ctx, &messengertypes.SystemInfo_Request{})
if err != nil {
return errcode.TODO.Wrap(err)
}
if ret.Messenger.ProtocolInSameProcess {
ret.Messenger.Process = nil
}
if anonymizeFlag {
if ret.Messenger != nil && ret.Messenger.Process != nil {
redactStringPtr(&ret.Messenger.Process.HostName)
redactStringPtr(&ret.Messenger.Process.WorkingDir)
}
if ret.Protocol != nil && ret.Protocol.Process != nil {
redactStringPtr(&ret.Protocol.Process.HostName)
redactStringPtr(&ret.Protocol.Process.WorkingDir)
}
}
if refreshEveryFlag == 0 {
fmt.Println(godev.PrettyJSONPB(ret))
break
}
// clear screen
print("\033[H\033[2J")
fmt.Println(godev.PrettyJSONPB(ret))
time.Sleep(refreshEveryFlag)
}
return nil
},
}
}
func redactStringPtr(ptr *string) {
if ptr != nil && *ptr != "" {
*ptr = "REDACTED"
}
}