44 "context"
55 "errors"
66 "fmt"
7- "github.com/showwin/speedtest-go/speedtest/transport"
8- "gopkg.in/alecthomas/kingpin.v2"
97 "io"
108 "log"
119 "os"
@@ -15,6 +13,9 @@ import (
1513 "sync/atomic"
1614 "time"
1715
16+ "github.com/showwin/speedtest-go/speedtest/transport"
17+ "gopkg.in/alecthomas/kingpin.v2"
18+
1819 "github.com/showwin/speedtest-go/speedtest"
1920)
2021
2425 customURL = kingpin .Flag ("custom-url" , "Specify the url of the server instead of fetching from speedtest.net." ).String ()
2526 savingMode = kingpin .Flag ("saving-mode" , "Test with few resources, though low accuracy (especially > 30Mbps)." ).Bool ()
2627 jsonOutput = kingpin .Flag ("json" , "Output results in json format." ).Bool ()
28+ jsonlOutput = kingpin .Flag ("jsonl" , "Output results in jsonl format (one json object per line)." ).Bool ()
2729 unixOutput = kingpin .Flag ("unix" , "Output results in unix like format." ).Bool ()
2830 location = kingpin .Flag ("location" , "Change the location with a precise coordinate (format: lat,lon)." ).String ()
2931 city = kingpin .Flag ("city" , "Change the location with a predefined city label." ).String ()
@@ -58,7 +60,7 @@ func main() {
5860 log .SetOutput (io .Discard )
5961
6062 // start unix output for saving mode by default.
61- if * savingMode && ! * jsonOutput && ! * unixOutput {
63+ if * savingMode && ! * jsonOutput && ! * jsonlOutput && ! * unixOutput {
6264 * unixOutput = true
6365 }
6466
@@ -84,7 +86,7 @@ func main() {
8486 }
8587
8688 // 1. retrieving user information
87- taskManager := InitTaskManager (* jsonOutput , * unixOutput )
89+ taskManager := InitTaskManager (* jsonOutput || * jsonlOutput , * unixOutput )
8890 taskManager .AsyncRun ("Retrieving User Information" , func (task * Task ) {
8991 u , err := speedtestClient .FetchUserInfo ()
9092 task .CheckError (err )
@@ -133,7 +135,7 @@ func main() {
133135
134136 // 3. test each selected server with ping, download and upload.
135137 for _ , server := range targets {
136- if ! * jsonOutput {
138+ if ! * jsonOutput && ! * jsonlOutput {
137139 fmt .Println ()
138140 }
139141 taskManager .Println ("Test Server: " + server .String ())
@@ -216,7 +218,7 @@ func main() {
216218 }
217219 packetLossAnalyzerCancel ()
218220 blocker .Wait ()
219- if ! * jsonOutput {
221+ if ! * jsonOutput && ! * jsonlOutput {
220222 taskManager .Println (server .PacketLoss .String ())
221223 }
222224 taskManager .Reset ()
@@ -230,6 +232,14 @@ func main() {
230232 panic (errMarshal )
231233 }
232234 fmt .Print (string (json ))
235+ } else if * jsonlOutput {
236+ for _ , server := range targets {
237+ json , errMarshal := speedtestClient .JSONL (server )
238+ if errMarshal != nil {
239+ panic (errMarshal )
240+ }
241+ fmt .Println (string (json ))
242+ }
233243 }
234244}
235245
@@ -321,7 +331,7 @@ func parseProto(str string) speedtest.Proto {
321331}
322332
323333func AppInfo () {
324- if ! * jsonOutput {
334+ if ! * jsonOutput && ! * jsonlOutput {
325335 fmt .Println ()
326336 fmt .Printf (" speedtest-go v%s (git-%s) @showwin\n " , speedtest .Version (), commit )
327337 fmt .Println ()
0 commit comments