Skip to content

Commit

Permalink
cli4 now accepts more than one call on the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Apr 9, 2020
1 parent 1c70a8e commit 4e67548
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ cli4: /zones - 9103 Unknown X-Auth-Key or X-Auth-Email
$
```

More than one call can be done on the same command line. In this mode, the connection is preserved between calls.
```
$ cli4 /user/organizations /user/invites
...
$
```
Note that the output is presently two JSON structures one after the other - so less useful that you may think.

Finally, a command that provides more than one error response.
This is simulated by passing an invalid IPv4 address to a DNS record creation.

Expand Down
40 changes: 27 additions & 13 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def run_command(cf, method, command, params=None, content=None, files=None):
else:
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
except Exception as e:
sys.exit('cli4: /%s - %s' % (command, e))
sys.stderr.write('cli4: /%s - %s\n' % (command, e))
raise e
cmd.append(':' + identifier1)
elif identifier2 is None:
if len(element) in [32, 40, 48] and hex_only.match(element):
Expand All @@ -95,7 +96,8 @@ def run_command(cf, method, command, params=None, content=None, files=None):
else:
raise Exception("/%s/%s :NOT CODED YET" % ('/'.join(cmd), element))
except Exception as e:
sys.exit('cli4: /%s - %s' % (command, e))
sys.stderr.write('cli4: /%s - %s\n' % (command, e))
raise e
# identifier2 may be an array - this needs to be dealt with later
if isinstance(identifier2, list):
cmd.append(':' + '[' + ','.join(identifier2) + ']')
Expand All @@ -109,20 +111,23 @@ def run_command(cf, method, command, params=None, content=None, files=None):
elif waf_rules.match(element):
identifier3 = element
else:
sys.exit("/%s/%s :NOT CODED YET 3" % ('/'.join(cmd), element))
sys.stderr.write('/%s/%s :NOT CODED YET 3\n' % ('/'.join(cmd), element))
raise e
else:
try:
m = getattr(m, element)
cmd.append(element)
except AttributeError:
# the verb/element was not found
if len(cmd) == 0:
sys.exit('cli4: /%s - not found' % (element))
sys.stderr.write('cli4: /%s - not found\n' % (element))
else:
sys.exit('cli4: /%s/%s - not found' % ('/'.join(cmd), element))
sys.stderr.write('cli4: /%s/%s - not found\n' % ('/'.join(cmd), element))
raise e

if content and params:
sys.exit('cli4: /%s - content and params not allowed together' % (command))
sys.stderr.write('cli4: /%s - content and params not allowed together\n' % (command))
raise Exception
if content:
params = content

Expand Down Expand Up @@ -163,11 +168,14 @@ def run_command(cf, method, command, params=None, content=None, files=None):
# more than one error returned by the API
for x in e:
sys.stderr.write('cli4: /%s - %d %s\n' % (command, x, x))
sys.exit('cli4: /%s - %d %s' % (command, e, e))
sys.stderr.write('cli4: /%s - %d %s\n' % (command, e, e))
raise e
except CloudFlare.exceptions.CloudFlareInternalError as e:
sys.exit('cli4: InternalError: /%s - %d %s' % (command, e, e))
sys.stderr.write('cli4: InternalError: /%s - %d %s\n' % (command, e, e))
raise e
except Exception as e:
sys.exit('cli4: /%s - %s - api error' % (command, e))
sys.stderr.write('cli4: /%s - %s - api error\n' % (command, e))
raise e

results.append(r)
return results
Expand Down Expand Up @@ -379,16 +387,22 @@ def do_it(args):
(tag_string, value_string))

# what's left is the command itself
if len(args) != 1:
if len(args) < 1:
sys.exit(usage)
command = args[0]
commands = args

try:
cf = CloudFlare.CloudFlare(debug=verbose, raw=raw, profile=profile)
except Exception as e:
sys.exit(e)
results = run_command(cf, method, command, params, content, files)
write_results(results, output)

for command in commands:
try:
results = run_command(cf, method, command, params, content, files)
write_results(results, output)
except Exception as e:
if len(commands) > 1:
continue

def cli4(args):
"""Cloudflare API via command line"""
Expand Down

0 comments on commit 4e67548

Please sign in to comment.