Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit c2224e4

Browse files
committed
NDJSON output supported - used by Enterprise Logs
1 parent ead7bbd commit c2224e4

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

cli4/cli4.man

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cli4 \- Command line access to CloudFlare v4 API
1111
[\fB\-q\fR|\fB\-\-quiet]
1212
[\fB\-j\fR|\fB\-\-json]
1313
[\fB\-y\fR|\fB\-\-yaml]
14+
[\fB\-n\fR|\fB\-\-ndjson]
1415
[\fBitem\fR=\fIvalue\fR ...]
1516
[\fB\-G\fR|\fB\-\-get]
1617
[\fB\-P\fR|\fB\-\-patch]
@@ -37,6 +38,8 @@ Don't output any JSON/YAML responses.
3738
Output response data in JSON format (the default).
3839
.IP "[\-y, \-\-yaml]"
3940
Output response data in YAML format (if yaml package installed).
41+
.IP "[\-n, \-\-ndjson]"
42+
Output response data in NDJSON format (if jsonlines package installed).
4043
.IP "\-\-get"
4144
Send HTTP request as a \fBGET\fR (the default).
4245
.IP "\-\-patch"

cli4/cli4.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import yaml
1010
except ImportError:
1111
yaml = None
12+
try:
13+
import jsonlines
14+
except ImportError:
15+
jsonlines = None
1216

1317
from . import converters
1418

@@ -185,6 +189,12 @@ def write_results(results, output):
185189
ensure_ascii=False)
186190
if output == 'yaml':
187191
results = yaml.safe_dump(results)
192+
if output == 'ndjson':
193+
# NDJSON support seems like a hack. There has to be a better way
194+
writer = jsonlines.Writer(sys.stdout)
195+
writer.write_all(results)
196+
writer.close()
197+
return
188198

189199
sys.stdout.write(results)
190200
if not results.endswith('\n'):
@@ -200,7 +210,7 @@ def do_it(args):
200210
method = 'GET'
201211

202212
usage = ('usage: cli4 '
203-
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] '
213+
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] [-n|ndjson]'
204214
+ '[-r|--raw] '
205215
+ '[-d|--dump] '
206216
+ '[--get|--patch|--post|--put|--delete] '
@@ -212,7 +222,7 @@ def do_it(args):
212222
'VhvqjyrdGPOUD',
213223
[
214224
'version',
215-
'help', 'verbose', 'quiet', 'json', 'yaml',
225+
'help', 'verbose', 'quiet', 'json', 'yaml', 'ndjson',
216226
'raw',
217227
'dump',
218228
'get', 'patch', 'post', 'put', 'delete'
@@ -234,6 +244,10 @@ def do_it(args):
234244
if yaml is None:
235245
exit('cli4: install yaml support')
236246
output = 'yaml'
247+
elif opt in ('-n', '--ndjson'):
248+
if jsonlines is None:
249+
exit('cli4: install jsonlines support')
250+
output = 'ndjson'
237251
elif opt in ('-r', '--raw'):
238252
raw = True
239253
elif opt in ('-d', '--dump'):

0 commit comments

Comments
 (0)