sindresorhus / fast-cli

Test your download and upload speed using fast.com
MIT License
2.6k stars 111 forks source link

Add `--json` flag #56

Closed seanmcne closed 3 years ago

seanmcne commented 3 years ago

output examples (note: I used node directly to debug locally)

I also added several common properties on the fast.com webpage currently to help improve data captured.

for json human readable output

node .\cli.js --json-pretty -u
{
     "downloadSpeed": 29,
     "downloadUnit": "Mbps",
     "downloadedMb": 30,
     "uploadSpeed": 13,
     "uploadUnit": "Mbps",
     "uploadedMb": 40,
     "latency": 44,
     "bufferBloat": 75,
     "userLocation": "Chicago, US",
     "userIp": "1.2.3.4",
     "isDone": true
}

for json only output

node .\cli.js --json -u        
{"downloadSpeed":32,"downloadUnit":"Mbps","downloadedMb":80,"uploadSpeed":10,"uploadUnit":"Mbps","uploadedMb":30,"latency":52,"bufferBloat":75,"userLocation":"Chicago, US","userIp":"1.2.3.4","isDone":true}
sindresorhus commented 3 years ago

I don't see the point of 2 JSON flags. Make it just --json and pretty.

seanmcne commented 3 years ago

I don't see the point of 2 JSON flags. Make it just --json and pretty.

I assume there are some folks that want to use this in automation and logging and wouldn't want the extra spaces and tabs, whereas some may want it human readable (it's actually matching the options that speedtest.net has for their CLI).

My previous experience with a similar command line tool was for speedtest-cli - here is the command help output for that cli tool which is what I patterned json and json-pretty from:

Speedtest by Ookla is the official command line client for testing the speed and performance of your internet connection.

Version: speedtest 1.0.0.2

Usage: speedtest [<options>]
  -h, --help                        Print usage information
  -V, --version                     Print version number
  -L, --servers                     List nearest servers
  -s, --server-id=#                 Specify a server from the server list using its id
  -I, --interface=ARG               Attempt to bind to the specified interface when connecting to servers
  -i, --ip=ARG                      Attempt to bind to the specified IP address when connecting to servers
  -o, --host=ARG                    Specify a server, from the server list, using its host's fully qualified domain name
  -p, --progress=yes|no             Enable or disable progress bar (Note: only available for 'human-readable'
                                    or 'json' and defaults to yes when interactive)
  -P, --precision=#                 Number of decimals to use (0-8, default=2)
  -f, --format=ARG                  Output format (see below for valid formats)
  -u, --unit[=ARG]                  Output unit for displaying speeds (Note: this is only applicable
                                    for ‘human-readable’ output format and the default unit is Mbps)
  -a                                Shortcut for [-u auto-decimal-bits]
  -A                                Shortcut for [-u auto-decimal-bytes]
  -b                                Shortcut for [-u auto-binary-bits]
  -B                                Shortcut for [-u auto-binary-bytes]
      --selection-details           Show server selection details
  -v                                Logging verbosity. Specify multiple times for higher verbosity
      --output-header               Show output header for CSV and TSV formats

 Valid output formats: human-readable (default), csv, tsv, json, jsonl, json-pretty

 Machine readable formats (csv, tsv, json, jsonl, json-pretty) use bytes as the unit of measure with max precision

 Valid units for [-u] flag: 
   Decimal prefix, bits per second:  bps, kbps, Mbps, Gbps
   Decimal prefix, bytes per second: B/s, kB/s, MB/s, GB/s
   Binary prefix, bits per second:   kibps, Mibps, Gibps
   Binary prefix, bytes per second:  kiB/s, MiB/s, GiB/s
   Auto-scaled prefix: auto-binary-bits, auto-binary-bytes, auto-decimal-bits, auto-decimal-bytes
sindresorhus commented 3 years ago

I assume there are some folks that want to use this in automation and logging and wouldn't want the extra spaces and tabs, whereas some may want it human-readable (it's actually matching the options that speedtest.net has for their CLI).

Logging is a good point. Let's only do the one-line version then. I don't really see the point of human-readable JSON output anyway, as we already have a nice output for humans. If someone needs a nice JSON output they can pipe the output through some JSON formatter.

sindresorhus commented 3 years ago

My previous experience with a similar command line tool was for speedtest-cli - here is the command help output for that cli tool which is what I patterned json and json-pretty from:

I prefer to keep my CLIs minimal.

seanmcne commented 3 years ago

I assume there are some folks that want to use this in automation and logging and wouldn't want the extra spaces and tabs, whereas some may want it human-readable (it's actually matching the options that speedtest.net has for their CLI).

Logging is a good point. Let's only do the one-line version then. I don't really see the point of human-readable JSON output anyway, as we already have a nice output for humans. If someone needs a nice JSON output they can pipe the output through some JSON formatter.

Sounds good - I just adjusted cli.js. Let me know what you thank - thx!

sindresorhus commented 3 years ago

@seanmcne I would recommend looking over your own diff after doing changes. I think you could have caught some of the things I commented yourself. 👍🏻

sindresorhus commented 3 years ago

Closing in favor of #58