metal-cli icon indicating copy to clipboard operation
metal-cli copied to clipboard

csv output in all commands that produce tables

Open vielmetti opened this issue 3 years ago • 3 comments

What problem are you facing?

For reporting purposes, it's useful to take a data extract from the Packet system and then import it into a spreadsheet for further analysis.

https://github.com/olekukonko/tablewriter/issues/80 describes how to use the tablewriter package to produce CSV output.

How could the Packet CLI help solve this problem?

Add a --csv or --output csv or similar option to format tabular output as tables, with a design goal to import into Excel or Google Sheets.

Alternatively, document how to reformat the JSON or YAML output into tabular CSV format.

vielmetti avatar Aug 31 '20 01:08 vielmetti

It is possible to get CSV (or near enough to CSV) output by piping the JSON output into jq, though getting exactly what you want might be difficult with that tool.

An example:

packet device get -p $PACKETPROJECT --include created_at,ip_addresses.address -j | jq -c '.[] |[.hostname,.id,.created_at,.ip_addresses[].address]'

with this output

["freebsd-ewr1","116abfa3-b1c7-xxxx-a559-3a48e691a419","2020-08-30T02:04:30Z","147.75.67.3","2604:1380:0:4300::3","10.99.102.3"]
["debian10-ewr1","8b6695c5-xxxx-4c28-94a2-922fda96aae7","2020-08-30T02:05:37Z","147.75.67.1","2604:1380:0:4300::5","10.99.102.5"]
["qualcomm-01","18cc6bf8-xxxx-4025-9e01-f7cd894ae6d2","2020-05-21T04:52:12Z","147.75.47.222","2604:1380:4111:9d00::1","10.32.50.129"]
["bastion-01","6105741f-14ee-xxxx-8fd9-e580af929ba2","2020-05-20T16:44:22Z","147.75.76.43","2604:1380:0:4300::1","10.99.102.1"]
["mon1","2f07a39f-xxxx-4619-87f9-432be941c892","2020-06-15T11:37:32Z","139.178.68.41","2604:1380:1000:e800::1","10.88.127.1"]

(slightly redacted output)

vielmetti avatar Sep 09 '20 03:09 vielmetti

Similar results, perhaps better, with

packet device get -p $FREEBIES  -j | in2csv -f json | csvcut -c id,hostname,ip_addresses/0/address

using the in2csv and csvcut tools from the csvkit package.

vielmetti avatar Sep 09 '20 03:09 vielmetti

Here's an example of how you can use the JSON format with jq to get custom tables output (thanks @dlotterman):

$ metal device list -o json | \
  jq -r '.[] | .id + "\t" + (.ip_addresses[]| select ((.address_family==4) and .public==true) | .address|tostring) + "\t" + .state + "\t" + .hostname'
3a500289-10d1-49aa-8214-6d0b55818860	203.0.113.54	active	test

displague avatar Mar 06 '23 13:03 displague