@@ -33,7 +33,7 @@ def cli4(args):
3333 + '[-r|--raw] '
3434 + '[-d|--dump] '
3535 + '[--get|--patch|--post|--put|--delete] '
36- + '[item=value ...] '
36+ + '[item=value|item=@filename|@filename ...] '
3737 + '/command...' )
3838
3939 try :
@@ -86,12 +86,26 @@ def cli4(args):
8686 digits_only = re .compile ('^-?[0-9]+$' )
8787 floats_only = re .compile ('^-?[0-9.]+$' )
8888
89- # next grab the params. These are in the form of tag=value
89+ # next grab the params. These are in the form of tag=value or =value or @filename
9090 params = None
9191 content = None
9292 files = None
93- while len (args ) > 0 and '=' in args [0 ]:
94- tag_string , value_string = args .pop (0 ).split ('=' , 1 )
93+ while len (args ) > 0 and ('=' in args [0 ] or args [0 ][0 ] == '@' ):
94+ arg = args .pop (0 )
95+ if arg [0 ] == '@' :
96+ if method != 'PUT' :
97+ exit ('cli4: %s - raw file upload only with PUT' % (filename ))
98+ filename = arg [1 :]
99+ try :
100+ if filename == '-' :
101+ content = sys .stdin .read ()
102+ else :
103+ with open (filename , 'r' ) as f :
104+ content = f .read ()
105+ except IOError :
106+ exit ('cli4: %s - file open failure' % (filename ))
107+ continue
108+ tag_string , value_string = arg .split ('=' , 1 )
95109 if value_string .lower () == 'true' :
96110 value = True
97111 elif value_string .lower () == 'false' :
@@ -118,25 +132,16 @@ def cli4(args):
118132 elif value_string [0 ] is '@' :
119133 filename = value_string [1 :]
120134 # a file to be uploaded - used in dns_records/import - only via POST
121- if tag_string == '' :
122- if method != 'PUT' :
123- exit ('cli4: %s - raw file upload only with PUT' % (filename ))
124- try :
125- if filename == '-' :
126- content = sys .stdin .read ()
127- else :
128- with open (filename , 'r' ) as f :
129- content = f .read ()
130- except :
131- exit ('cli4: %s - file open failure' % (filename ))
132- else :
133- if method != 'POST' :
134- exit ('cli4: %s=%s - file upload only with POST' % (tag_string , filename ))
135- files = {}
136- try :
135+ if method != 'POST' :
136+ exit ('cli4: %s=%s - file upload only with POST' % (tag_string , filename ))
137+ files = {}
138+ try :
139+ if filename == '-' :
140+ files [tag_string ] = sys .stdin
141+ else :
137142 files [tag_string ] = open (filename , 'rb' )
138- except :
139- exit ('cli4: %s=%s - file open failure' % (tag_string , filename ))
143+ except IOError :
144+ exit ('cli4: %s=%s - file open failure' % (tag_string , filename ))
140145 # no need for param code below
141146 continue
142147 else :
@@ -313,9 +318,16 @@ def cli4(args):
313318 # anything more complex (dict, list, etc) should be dumped as JSON/YAML
314319 if output == 'json' :
315320 try :
316- results = json .dumps (results , indent = 4 , sort_keys = True , ensure_ascii = False , encoding = 'utf8' )
321+ results = json .dumps (results ,
322+ indent = 4 ,
323+ sort_keys = True ,
324+ ensure_ascii = False ,
325+ encoding = 'utf8' )
317326 except TypeError as e :
318- results = json .dumps (results , indent = 4 , sort_keys = True , ensure_ascii = False )
327+ results = json .dumps (results ,
328+ indent = 4 ,
329+ sort_keys = True ,
330+ ensure_ascii = False )
319331 if output == 'yaml' :
320332 results = yaml .safe_dump (results )
321333
0 commit comments