Skip to content

Commit bf4de12

Browse files
committed
Reduce complexity in deis.py's main() by refactoring command dispatch lines
1 parent 7b1cc2c commit bf4de12

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

client/deis.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,28 @@ def parse_args(cmd):
19991999
return cmd, help_flag
20002000

20012001

2002+
def _dispatch_cmd(method, args):
2003+
try:
2004+
method(args)
2005+
except requests.exceptions.ConnectionError as err:
2006+
print("Couldn't connect to the Deis Controller. Make sure that the Controller URI is \
2007+
correct and the server is running.")
2008+
sys.exit(1)
2009+
except EnvironmentError as err:
2010+
raise DocoptExit(err.message)
2011+
except ResponseError as err:
2012+
resp = err.message
2013+
print('{} {}'.format(resp.status_code, resp.reason))
2014+
try:
2015+
msg = resp.json()
2016+
if 'detail' in msg:
2017+
msg = "Detail:\n{}".format(msg['detail'])
2018+
except:
2019+
msg = resp.text
2020+
print(msg)
2021+
sys.exit(1)
2022+
2023+
20022024
def main():
20032025
"""
20042026
Create a client, parse the arguments received on the command line, and
@@ -2026,25 +2048,7 @@ def main():
20262048
if 'Usage: ' in docstring:
20272049
args.update(docopt(docstring))
20282050
# dispatch the CLI command
2029-
try:
2030-
method(args)
2031-
except requests.exceptions.ConnectionError as err:
2032-
print("Couldn't connect to the Deis Controller. Make sure that the Controller URI is \
2033-
correct and the server is running.")
2034-
sys.exit(1)
2035-
except EnvironmentError as err:
2036-
raise DocoptExit(err.message)
2037-
except ResponseError as err:
2038-
resp = err.message
2039-
print('{} {}'.format(resp.status_code, resp.reason))
2040-
try:
2041-
msg = resp.json()
2042-
if 'detail' in msg:
2043-
msg = "Detail:\n{}".format(msg['detail'])
2044-
except:
2045-
msg = resp.text
2046-
print(msg)
2047-
sys.exit(1)
2051+
_dispatch_cmd(method, args)
20482052

20492053

20502054
if __name__ == '__main__':

0 commit comments

Comments
 (0)