@@ -18,8 +18,6 @@ import (
18
18
"github.com/coder/serpent"
19
19
)
20
20
21
- // clui will either format this as json,
22
- // or our speedtestTableFormatter will format this as a table
23
21
type speedtestResult struct {
24
22
Overall speedtestResultInterval `json:"overall"`
25
23
Intervals []speedtestResultInterval `json:"intervals"`
@@ -36,50 +34,33 @@ type speedtestTableItem struct {
36
34
Throughput string `table:"Throughput"`
37
35
}
38
36
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
-
75
37
func (r * RootCmd ) speedtest () * serpent.Command {
76
38
var (
77
39
direct bool
78
40
duration time.Duration
79
41
direction string
80
42
pcapFile string
81
43
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
+ }),
83
64
cliui .JSONFormat (),
84
65
)
85
66
)
0 commit comments