Skip to content

Commit ed03cec

Browse files
committed
WIP
1 parent a93efdb commit ed03cec

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed

cli/speedtest.go

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"github.com/coder/serpent"
1919
)
2020

21-
// clui will either format this as json,
22-
// or our speedtestTableFormatter will format this as a table
2321
type speedtestResult struct {
2422
Overall speedtestResultInterval `json:"overall"`
2523
Intervals []speedtestResultInterval `json:"intervals"`
@@ -36,50 +34,33 @@ type speedtestTableItem struct {
3634
Throughput string `table:"Throughput"`
3735
}
3836

39-
// Attaches a CLI arg with table output
40-
type speedtestTableFormatter struct {
41-
tableFormatter cliui.OutputFormat
42-
}
43-
44-
var _ cliui.OutputFormat = &speedtestTableFormatter{}
45-
46-
func (*speedtestTableFormatter) ID() string {
47-
return "table"
48-
}
49-
50-
func (f *speedtestTableFormatter) AttachOptions(opts *serpent.OptionSet) {
51-
f.tableFormatter = cliui.TableFormat([]speedtestTableItem{}, []string{"Interval", "Throughput"})
52-
f.tableFormatter.AttachOptions(opts)
53-
}
54-
55-
func (f *speedtestTableFormatter) Format(ctx context.Context, data any) (string, error) {
56-
res, ok := data.(speedtestResult)
57-
if !ok {
58-
panic("attempted speedtest table format with an invalid result")
59-
}
60-
tableRows := make([]any, len(res.Intervals)+2)
61-
for i, r := range res.Intervals {
62-
tableRows[i] = speedtestTableItem{
63-
Interval: fmt.Sprintf("%.2f-%.2f sec", r.StartTimeSeconds, r.EndTimeSeconds),
64-
Throughput: fmt.Sprintf("%.4f Mbits/sec", r.ThroughputMbits),
65-
}
66-
}
67-
tableRows[len(res.Intervals)] = cliui.TableSeparator{}
68-
tableRows[len(res.Intervals)+1] = speedtestTableItem{
69-
Interval: "Total",
70-
Throughput: fmt.Sprintf("%.4f Mbits/sec", res.Overall.ThroughputMbits),
71-
}
72-
return f.tableFormatter.Format(ctx, tableRows)
73-
}
74-
7537
func (r *RootCmd) speedtest() *serpent.Command {
7638
var (
7739
direct bool
7840
duration time.Duration
7941
direction string
8042
pcapFile string
8143
formatter = cliui.NewOutputFormatter(
82-
&speedtestTableFormatter{},
44+
cliui.ChangeFormatterData(cliui.TableFormat([]speedtestTableItem{}, []string{"Interval", "Throughput"}), func(data any) (any, error) {
45+
res, ok := data.(speedtestResult)
46+
if !ok {
47+
// This should never happen
48+
return "", xerrors.Errorf("expected speedtestResult, got %T", data)
49+
}
50+
tableRows := make([]any, len(res.Intervals)+2)
51+
for i, r := range res.Intervals {
52+
tableRows[i] = speedtestTableItem{
53+
Interval: fmt.Sprintf("%.2f-%.2f sec", r.StartTimeSeconds, r.EndTimeSeconds),
54+
Throughput: fmt.Sprintf("%.4f Mbits/sec", r.ThroughputMbits),
55+
}
56+
}
57+
tableRows[len(res.Intervals)] = cliui.TableSeparator{}
58+
tableRows[len(res.Intervals)+1] = speedtestTableItem{
59+
Interval: "Total",
60+
Throughput: fmt.Sprintf("%.4f Mbits/sec", res.Overall.ThroughputMbits),
61+
}
62+
return tableRows, nil
63+
}),
8364
cliui.JSONFormat(),
8465
)
8566
)

0 commit comments

Comments
 (0)