Skip to content

Commit

Permalink
allow binary files for file upload via --binary flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Aug 7, 2022
1 parent 10b30c9 commit 2ba207a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def do_it(args):
raw = False
dump = False
dump_from_web = False
binary_file = False
profile = None
method = 'GET'

Expand All @@ -281,19 +282,21 @@ def do_it(args):
+ '[-r|--raw] '
+ '[-d|--dump] '
+ '[-a|--api] '
+ '[-b|--binary] '
+ '[-p|--profile profile-name] '
+ '[--get|--patch|--post|--put|--delete] '
+ '[item=value|item=@filename|@filename ...] '
+ '/command ...')

try:
opts, args = getopt.getopt(args,
'Vhvqjyrdap:GPOUD',
'Vhvqjyrdabp:GPOUD',
[
'version',
'help', 'verbose', 'quiet', 'json', 'yaml', 'ndjson',
'raw',
'dump', 'api',
'binary',
'profile=',
'get', 'patch', 'post', 'put', 'delete'
])
Expand Down Expand Up @@ -326,6 +329,8 @@ def do_it(args):
dump = True
elif opt in ('-a', '--api'):
dump_from_web = True
elif opt in ('-b', '--binary'):
binary_file = True
elif opt in ('-G', '--get'):
method = 'GET'
elif opt in ('-P', '--patch'):
Expand Down Expand Up @@ -363,10 +368,17 @@ def do_it(args):
sys.exit('cli4: %s - raw file upload only with PUT or POST' % (filename))
try:
if filename == '-':
content = sys.stdin.read()
if binary_file:
content = sys.stdin.buffer.read()
else:
content = sys.stdin.read()
else:
with open(filename, 'r') as f:
content = f.read()
if binary_file:
with open(filename, 'rb') as f:
content = f.read()
else:
with open(filename, 'r') as f:
content = f.read()
except IOError:
sys.exit('cli4: %s - file open failure' % (filename))
continue
Expand Down

0 comments on commit 2ba207a

Please sign in to comment.