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

Commit 453fed3

Browse files
committed
handle raw byte output either via --image flag or if return from api is bytes
1 parent e08610b commit 453fed3

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

cli4/cli4.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,11 @@ def write_results(results, output):
320320
if len(results) == 1:
321321
results = results[0]
322322

323-
if isinstance(results, str):
323+
if isinstance(results, str) or isinstance(results, (bytes, bytearray)):
324324
# if the results are a simple string, then it should be dumped directly
325325
# this is only used for /zones/:id/dns_records/export presently
326+
# or
327+
# output is image or audio or video or something like that so we dump directly
326328
pass
327329
else:
328330
# anything more complex (dict, list, etc) should be dumped as JSON/YAML
@@ -350,14 +352,18 @@ def write_results(results, output):
350352
pass
351353
return
352354
else:
353-
# Internal error
354-
pass
355+
# None of the above, so pass thru results except something in byte form
356+
if not isinstance(results, (bytes, bytearray)):
357+
results = str(results)
355358

356359
if results:
357360
try:
358-
sys.stdout.write(results)
359-
if not results.endswith('\n'):
360-
sys.stdout.write('\n')
361+
if isinstance(results, (bytes, bytearray)):
362+
sys.stdout.buffer.write(results)
363+
else:
364+
sys.stdout.write(results)
365+
if not results.endswith('\n'):
366+
sys.stdout.write('\n')
361367
except (BrokenPipeError, IOError):
362368
pass
363369

@@ -377,7 +383,7 @@ def do_it(args):
377383
usage = ('usage: cli4 '
378384
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] '
379385
+ '[-e|--examples] '
380-
+ '[-j|--json] [-y|--yaml] [-n|ndjson] '
386+
+ '[-j|--json] [-y|--yaml] [-n|--ndjson] [-i|--image]'
381387
+ '[-r|--raw] '
382388
+ '[-d|--dump] '
383389
+ '[-A|--openapi url] '
@@ -389,10 +395,10 @@ def do_it(args):
389395

390396
try:
391397
opts, args = getopt.getopt(args,
392-
'VhvqejyrdA:bp:GPOUD',
398+
'VhvqejynirdA:bp:GPOUD',
393399
[
394400
'version',
395-
'help', 'verbose', 'quiet', 'examples', 'json', 'yaml', 'ndjson',
401+
'help', 'verbose', 'quiet', 'examples', 'json', 'yaml', 'ndjson', 'image',
396402
'raw',
397403
'dump', 'openapi=',
398404
'binary',
@@ -424,6 +430,8 @@ def do_it(args):
424430
if not my_jsonlines.available():
425431
sys.exit('cli4: install jsonlines support')
426432
output = 'ndjson'
433+
elif opt in ('-i', '--image'):
434+
output = 'image'
427435
elif opt in ('-r', '--raw'):
428436
raw = True
429437
elif opt in ('-p', '--profile'):

0 commit comments

Comments
 (0)