tart icon indicating copy to clipboard operation
tart copied to clipboard

Machine friendly output

Open tomjn opened this issue 2 years ago • 1 comments

Tarts CLI commands output human friendly text, but for automation and integrations it's better to print in a machine readable format such as JSON or XML.

To this end the addition of a general --json parameter for all CLI commands would be a great help

tomjn avatar Nov 04 '22 15:11 tomjn

Do you have particular commands that need machine readable output? There are couple of services that already integrated Tart as is since the outputs are already pretty simple. For example, tart ip just returns a single like so it's possible to do things like ssh admin@$(tart ip ventura-base).

fkorotkov avatar Nov 06 '22 00:11 fkorotkov

+1

This commit https://github.com/cirruslabs/tart/commit/c9caeab098bd4551735b441032cc40a332df1f02#diff-6279a63c53e5ce2ed8800fb80676147b5c5976824b5bc982640295a32eb3a683R51 broke our (GitLab) parsing of tart list because it added a space before each tab. We are fixing it by being a little more robust with our parsing: https://gitlab.com/gitlab-org/fleeting/nesting/-/merge_requests/4 but it still isn't 100% correct.

A JSON output format would really help here.

josephburnett avatar Jan 17 '23 17:01 josephburnett

Sorry to hear that @josephburnett! It seems overtime there was a need for JSON output only for list command. ip and get already return machine readable output.

It seems this issue can be addressed by a tart list --json which will output for each local or remote VM a line with a JSON object that contains all available information like name, settings, size, etc.

fkorotkov avatar Jan 17 '23 18:01 fkorotkov

@fkorotkov is that in an upcoming release? tart list --json doesn't work in the current release and there's no --json parameter in the help text

tomjn avatar Jan 17 '23 18:01 tomjn

No big deal. If tart was written in golang I would send you a pull request instead of an issue ;)

A more standard way to implement tart list --json would be to emit a single JSON list with objects for each VM. Most JSON readers expect a single value rather than line separated values. Not very Unix-like, but it's easiest from a tooling perspective.

josephburnett avatar Jan 17 '23 18:01 josephburnett

@tomjn tart list --json is a proposal for a change after which we can close this issue.

@josephburnett with a single value it can even be pretty printed for human eyes. 🤔

fkorotkov avatar Jan 17 '23 19:01 fkorotkov

@fkorotkov ok but if it is implemented, the entire response should be a single JSON response

for example this:

[
    { ... },
    { ... }
]

Not this:

{ ... }
{ ... }

Doing that immediately eliminates CLI commands such as jq, or piping, and forces everybody to write custom code to reprocess and normalise the output before it can be used.

Ideally, Tart internally would return data that can then be printed either as JSON or XML or human readable text, rather than printing it directly as it figures it out.

vms = getvms( filteroptions )
if output is json
    print json encoded ( vms )
else
    foreach vm
       print vm line etc

This also makes unit tests possible

tomjn avatar Jan 17 '23 19:01 tomjn

I'd also suggest an --format parameter so --format="json" for future proofing

tomjn avatar Jan 17 '23 19:01 tomjn