Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
NDJSON output supported - used by Enterprise Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Feb 25, 2018
1 parent ead7bbd commit c2224e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cli4/cli4.man
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cli4 \- Command line access to CloudFlare v4 API
[\fB\-q\fR|\fB\-\-quiet]
[\fB\-j\fR|\fB\-\-json]
[\fB\-y\fR|\fB\-\-yaml]
[\fB\-n\fR|\fB\-\-ndjson]
[\fBitem\fR=\fIvalue\fR ...]
[\fB\-G\fR|\fB\-\-get]
[\fB\-P\fR|\fB\-\-patch]
Expand All @@ -37,6 +38,8 @@ Don't output any JSON/YAML responses.
Output response data in JSON format (the default).
.IP "[\-y, \-\-yaml]"
Output response data in YAML format (if yaml package installed).
.IP "[\-n, \-\-ndjson]"
Output response data in NDJSON format (if jsonlines package installed).
.IP "\-\-get"
Send HTTP request as a \fBGET\fR (the default).
.IP "\-\-patch"
Expand Down
18 changes: 16 additions & 2 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import yaml
except ImportError:
yaml = None
try:
import jsonlines
except ImportError:
jsonlines = None

from . import converters

Expand Down Expand Up @@ -185,6 +189,12 @@ def write_results(results, output):
ensure_ascii=False)
if output == 'yaml':
results = yaml.safe_dump(results)
if output == 'ndjson':
# NDJSON support seems like a hack. There has to be a better way
writer = jsonlines.Writer(sys.stdout)
writer.write_all(results)
writer.close()
return

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

usage = ('usage: cli4 '
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] '
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] [-n|ndjson]'
+ '[-r|--raw] '
+ '[-d|--dump] '
+ '[--get|--patch|--post|--put|--delete] '
Expand All @@ -212,7 +222,7 @@ def do_it(args):
'VhvqjyrdGPOUD',
[
'version',
'help', 'verbose', 'quiet', 'json', 'yaml',
'help', 'verbose', 'quiet', 'json', 'yaml', 'ndjson',
'raw',
'dump',
'get', 'patch', 'post', 'put', 'delete'
Expand All @@ -234,6 +244,10 @@ def do_it(args):
if yaml is None:
exit('cli4: install yaml support')
output = 'yaml'
elif opt in ('-n', '--ndjson'):
if jsonlines is None:
exit('cli4: install jsonlines support')
output = 'ndjson'
elif opt in ('-r', '--raw'):
raw = True
elif opt in ('-d', '--dump'):
Expand Down

0 comments on commit c2224e4

Please sign in to comment.