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

Commit 1477dda

Browse files
committed
python2/python3 json output fixed for unicode, added cloudflare workers script/file uploading, added script name support
1 parent 7d5cf4d commit 1477dda

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

cli4/cli4.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def cli4(args):
8888

8989
# next grab the params. These are in the form of tag=value
9090
params = None
91+
content = None
9192
files = None
9293
while len(args) > 0 and '=' in args[0]:
9394
tag_string, value_string = args.pop(0).split('=', 1)
@@ -117,13 +118,25 @@ def cli4(args):
117118
elif value_string[0] is '@':
118119
filename = value_string[1:]
119120
# a file to be uploaded - used in dns_records/import - only via POST
120-
if method != 'POST':
121-
exit('cli4: %s=%s - file upload only with POST' % (tag_string, filename))
122-
files = {}
123-
try:
124-
files[tag_string] = open(filename, 'rb')
125-
except:
126-
exit('cli4: %s=%s - file open failure' % (tag_string, filename))
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:
137+
files[tag_string] = open(filename, 'rb')
138+
except:
139+
exit('cli4: %s=%s - file open failure' % (tag_string, filename))
127140
# no need for param code below
128141
continue
129142
else:
@@ -182,6 +195,9 @@ def cli4(args):
182195
if len(element) in [32, 40, 48] and hex_only.match(element):
183196
# raw identifier - lets just use it as-is
184197
identifier1 = element
198+
elif element[0] == ':':
199+
# raw string - used for workers script_name - use ::script_name
200+
identifier1 = element[1:]
185201
elif cmd[0] == 'certificates':
186202
# identifier1 = convert_certificates_to_identifier(cf, element)
187203
identifier1 = converters.convert_zones_to_identifier(cf, element)
@@ -204,6 +220,9 @@ def cli4(args):
204220
if len(element) in [32, 40, 48] and hex_only.match(element):
205221
# raw identifier - lets just use it as-is
206222
identifier2 = element
223+
elif element[0] == ':':
224+
# raw string - used for workers script_names
225+
identifier2 = element[1:]
207226
elif (cmd[0] and cmd[0] == 'zones') and (cmd[2] and cmd[2] == 'dns_records'):
208227
identifier2 = converters.convert_dns_record_to_identifier(cf,
209228
identifier1,
@@ -235,6 +254,11 @@ def cli4(args):
235254
else:
236255
exit('cli4: /%s/%s - not found' % ('/'.join(cmd), element))
237256

257+
if content and params:
258+
exit('cli4: /%s - content and params not allowed together' % (command))
259+
if content:
260+
params = content
261+
238262
results = []
239263
if identifier2 is None:
240264
identifier2 = [None]
@@ -284,17 +308,18 @@ def cli4(args):
284308
if isinstance(results, str):
285309
# if the results are a simple string, then it should be dumped directly
286310
# this is only used for /zones/:id/dns_records/export presently
287-
sys.stdout.write(results)
311+
pass
288312
else:
289313
# anything more complex (dict, list, etc) should be dumped as JSON/YAML
290314
if output == 'json':
291-
# json.dumps(results, sys.stdout, indent=4, sort_keys=True)
292-
# sys.stdout.write('\n')
293-
# sys.stdout.write(json.dumps(results, indent=4, sort_keys=True) + '\n')
294315
try:
295-
print(json.dumps(results, indent=4, sort_keys=True, ensure_ascii=False, encoding='utf8'))
316+
results = json.dumps(results, indent=4, sort_keys=True, ensure_ascii=False, encoding='utf8')
296317
except TypeError as e:
297-
print(json.dumps(results, indent=4, sort_keys=True, ensure_ascii=False))
318+
results = json.dumps(results, indent=4, sort_keys=True, ensure_ascii=False)
298319
if output == 'yaml':
299-
sys.stdout.write(yaml.safe_dump(results))
320+
results = yaml.safe_dump(results)
321+
322+
sys.stdout.write(results)
323+
if not results.endswith('\n'):
324+
sys.stdout.write('\n')
300325

0 commit comments

Comments
 (0)