Support NDJSON output mode
Problem to solve
When running in paginated mode, glab could potentially return hundreds or thousands of records.
Since the output is JSON, the entire dataset is returned in a single JSON array.
This causes multiple problems:
- The command has to paginate through all pages before returning a response. This can take a very long time.
- The JSON model is built up in memory in
glaband could run out of memory - Any downstream process connected to the stdout pipe for
glabwill need to parse the entire response in one go. This causes additional memory problems. - The downstream process needs to read the full dataset, and cannot prematurely terminate the stream (for example, once a particular record is found).
Proposal
Allow glab to return Newline Delimited JSON record (NDJSON) though an optional command line switch, such as --output ndjson. This blog entry describes some of the benefits of NDJSON over JSON: https://clue.engineering/2018/introducing-reactphp-ndjson
When --output ndjson is used, glab would emit NDJSON instead of a single array of values.
For example:
$ # using JSON output
$ glab api '...'
[
{ "item": 1 },
{ "item": 2 },
]
$ # using NDJSON output
$ glab api '...' --output ndjson
{ "item": 1 }
{ "item": 2 }
NDJSON works well with tools such as as jq.
For example, instead of running:
glab api 'todos' --paginate | jq '
.[] |
select(.target.state != "opened")|
.id
'
You could use the proposed --output ndjson (for example) and then use:
glab api 'todos' --paginate --output ndjson | jq '
select(.target.state != "opened")|
.id
'
This would have the benefit of using much less memory, since each item is emitted one at a time. Any downstream processes can start processing immediately, instead of having to wait for all paginated pages to complete before processing the response.