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

Commit 4e67548

Browse files
committed
cli4 now accepts more than one call on the command line
1 parent 1c70a8e commit 4e67548

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@ cli4: /zones - 9103 Unknown X-Auth-Key or X-Auth-Email
430430
$
431431
```
432432

433+
More than one call can be done on the same command line. In this mode, the connection is preserved between calls.
434+
```
435+
$ cli4 /user/organizations /user/invites
436+
...
437+
$
438+
```
439+
Note that the output is presently two JSON structures one after the other - so less useful that you may think.
440+
433441
Finally, a command that provides more than one error response.
434442
This is simulated by passing an invalid IPv4 address to a DNS record creation.
435443

cli4/cli4.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def run_command(cf, method, command, params=None, content=None, files=None):
7777
else:
7878
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
7979
except Exception as e:
80-
sys.exit('cli4: /%s - %s' % (command, e))
80+
sys.stderr.write('cli4: /%s - %s\n' % (command, e))
81+
raise e
8182
cmd.append(':' + identifier1)
8283
elif identifier2 is None:
8384
if len(element) in [32, 40, 48] and hex_only.match(element):
@@ -95,7 +96,8 @@ def run_command(cf, method, command, params=None, content=None, files=None):
9596
else:
9697
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
9798
except Exception as e:
98-
sys.exit('cli4: /%s - %s' % (command, e))
99+
sys.stderr.write('cli4: /%s - %s\n' % (command, e))
100+
raise e
99101
# identifier2 may be an array - this needs to be dealt with later
100102
if isinstance(identifier2, list):
101103
cmd.append(':' + '[' + ','.join(identifier2) + ']')
@@ -109,20 +111,23 @@ def run_command(cf, method, command, params=None, content=None, files=None):
109111
elif waf_rules.match(element):
110112
identifier3 = element
111113
else:
112-
sys.exit("/%s/%s :NOT CODED YET 3" % ('/'.join(cmd), element))
114+
sys.stderr.write('/%s/%s :NOT CODED YET 3\n' % ('/'.join(cmd), element))
115+
raise e
113116
else:
114117
try:
115118
m = getattr(m, element)
116119
cmd.append(element)
117120
except AttributeError:
118121
# the verb/element was not found
119122
if len(cmd) == 0:
120-
sys.exit('cli4: /%s - not found' % (element))
123+
sys.stderr.write('cli4: /%s - not found\n' % (element))
121124
else:
122-
sys.exit('cli4: /%s/%s - not found' % ('/'.join(cmd), element))
125+
sys.stderr.write('cli4: /%s/%s - not found\n' % ('/'.join(cmd), element))
126+
raise e
123127

124128
if content and params:
125-
sys.exit('cli4: /%s - content and params not allowed together' % (command))
129+
sys.stderr.write('cli4: /%s - content and params not allowed together\n' % (command))
130+
raise Exception
126131
if content:
127132
params = content
128133

@@ -163,11 +168,14 @@ def run_command(cf, method, command, params=None, content=None, files=None):
163168
# more than one error returned by the API
164169
for x in e:
165170
sys.stderr.write('cli4: /%s - %d %s\n' % (command, x, x))
166-
sys.exit('cli4: /%s - %d %s' % (command, e, e))
171+
sys.stderr.write('cli4: /%s - %d %s\n' % (command, e, e))
172+
raise e
167173
except CloudFlare.exceptions.CloudFlareInternalError as e:
168-
sys.exit('cli4: InternalError: /%s - %d %s' % (command, e, e))
174+
sys.stderr.write('cli4: InternalError: /%s - %d %s\n' % (command, e, e))
175+
raise e
169176
except Exception as e:
170-
sys.exit('cli4: /%s - %s - api error' % (command, e))
177+
sys.stderr.write('cli4: /%s - %s - api error\n' % (command, e))
178+
raise e
171179

172180
results.append(r)
173181
return results
@@ -379,16 +387,22 @@ def do_it(args):
379387
(tag_string, value_string))
380388

381389
# what's left is the command itself
382-
if len(args) != 1:
390+
if len(args) < 1:
383391
sys.exit(usage)
384-
command = args[0]
392+
commands = args
385393

386394
try:
387395
cf = CloudFlare.CloudFlare(debug=verbose, raw=raw, profile=profile)
388396
except Exception as e:
389397
sys.exit(e)
390-
results = run_command(cf, method, command, params, content, files)
391-
write_results(results, output)
398+
399+
for command in commands:
400+
try:
401+
results = run_command(cf, method, command, params, content, files)
402+
write_results(results, output)
403+
except Exception as e:
404+
if len(commands) > 1:
405+
continue
392406

393407
def cli4(args):
394408
"""Cloudflare API via command line"""

0 commit comments

Comments
 (0)