forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetcheck.go
More file actions
61 lines (49 loc) · 1.31 KB
/
netcheck.go
File metadata and controls
61 lines (49 loc) · 1.31 KB
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
package cli
import (
"context"
"encoding/json"
"fmt"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
"github.com/coder/coder/v2/codersdk"
)
func (r *RootCmd) netcheck() *clibase.Cmd {
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "netcheck",
Short: "Print network debug information for DERP and STUN",
Middleware: clibase.Chain(
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
ctx, cancel := context.WithTimeout(inv.Context(), 30*time.Second)
defer cancel()
connInfo, err := client.WorkspaceAgentConnectionInfoGeneric(ctx)
if err != nil {
return err
}
_, _ = fmt.Fprint(inv.Stderr, "Gathering a network report. This may take a few seconds...\n\n")
var report derphealth.Report
report.Run(ctx, &derphealth.ReportOptions{
DERPMap: connInfo.DERPMap,
})
raw, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}
n, err := inv.Stdout.Write(raw)
if err != nil {
return err
}
if n != len(raw) {
return xerrors.Errorf("failed to write all bytes to stdout; wrote %d, len %d", n, len(raw))
}
_, _ = inv.Stdout.Write([]byte("\n"))
return nil
},
}
cmd.Options = clibase.OptionSet{}
return cmd
}