Skip to content

Commit 2d5823e

Browse files
author
Gabriel Monroy
committed
fix ugly git remote not found stacktrace, misc cleanup #16
1 parent c014426 commit 2d5823e

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

client/deis/client.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
"""Usage: deis <command> [--formation=<formation>] [<args>...]
3+
"""Usage: deis <command> [--formation <formation>] [<args>...]
44
55
Options:
66
-h --help Show this help screen
@@ -207,6 +207,8 @@ def _dispatch(self, method, path, body=None,
207207

208208
def auth_register(self, args):
209209
"""
210+
Register a new user with a Deis controller
211+
210212
Usage: deis auth:register <controller> [--username=<username> --password=<password> --email=<email>]
211213
"""
212214
controller = args['<controller>']
@@ -485,52 +487,56 @@ def flavors_list(self, args):
485487

486488
def formations_create(self, args):
487489
"""
488-
Usage: deis formations:create --flavor=<flavor> [--image=<image> --id=<id>]
490+
Usage: deis formations:create [--flavor=<flavor> --id=<id>]
489491
"""
490492
body = {}
491-
for opt in ('--id', '--image'):
493+
for opt in ('--id',):
492494
o = args.get(opt)
493495
if o:
494496
body.update({opt.strip('-'): o})
497+
sys.stdout.write('Creating formation... ')
498+
sys.stdout.flush()
495499
response = self._dispatch('post', '/api/formations',
496500
json.dumps(body))
497501
if response.status_code == requests.codes.created: # @UndefinedVariable
498502
data = response.json()
499503
formation = data['id']
500-
print('Created ' + formation)
504+
print 'done, created {}'.format(formation)
505+
# add a git remote
501506
hostname = urlparse.urlparse(self._settings['controller']).netloc
507+
git_remote = 'git@{hostname}:{formation}.git'.format(**locals())
502508
try:
503509
subprocess.check_call(
504-
['git', 'remote', 'add', '-f', 'deis',
505-
'git@{hostname}:{formation}.git'.format(**locals())])
510+
['git', 'remote', 'add', '-f', 'deis', git_remote],
511+
stdout=subprocess.PIPE)
506512
except subprocess.CalledProcessError:
507513
sys.exit(1)
508-
print('Git remote added')
514+
print('Git remote deis added')
509515
else:
510516
print('Error!', response.text)
511517

512-
def formations_list(self, args):
518+
def formations_info(self, args):
513519
"""
514520
Usage: deis formations:info
515521
"""
516-
response = self._dispatch('get', '/api/formations')
522+
formation = args.get('<formation>')
523+
if not formation:
524+
formation = self._session.formation
525+
response = self._dispatch('get', '/api/formations/{}'.format(formation))
517526
if response.status_code == requests.codes.ok: # @UndefinedVariable
518-
data = response.json()
519-
for item in data['results']:
520-
print('{0[id]:<23}'.format(item))
527+
print(json.dumps(response.json(), indent=2))
521528
else:
522529
print('Error!', response.text)
523530

524-
def formations_info(self, args):
531+
def formations_list(self, args):
525532
"""
526533
Usage: deis formations:list
527534
"""
528-
formation = args.get('<formation>')
529-
if not formation:
530-
formation = self._session.formation
531-
response = self._dispatch('get', '/api/formations/{}'.format(formation))
535+
response = self._dispatch('get', '/api/formations')
532536
if response.status_code == requests.codes.ok: # @UndefinedVariable
533-
print(json.dumps(response.json(), indent=2))
537+
data = response.json()
538+
for item in data['results']:
539+
print('{0[id]:<23}'.format(item))
534540
else:
535541
print('Error!', response.text)
536542

@@ -562,7 +568,7 @@ def formations_destroy(self, args):
562568
subprocess.check_call(
563569
['git', 'remote', 'rm', 'deis'],
564570
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
565-
print('Git remote removed')
571+
print('Git remote deis removed')
566572
except subprocess.CalledProcessError:
567573
pass # ignore error
568574
else:
@@ -746,7 +752,11 @@ def layers_destroy(self, args):
746752
print('Error!', response.text)
747753

748754
def layers_list(self, args):
749-
"""List layers for this formation."""
755+
"""
756+
List layers for this formation.
757+
758+
Usage deis layers:list
759+
"""
750760
formation = args.get('--formation')
751761
if not formation:
752762
formation = self._session.formation
@@ -949,19 +959,25 @@ def main():
949959
docstring = trim(getattr(cli, cmd).__doc__)
950960
if 'Usage: ' in docstring:
951961
args.update(docopt(docstring))
952-
# dispatch based on primary command
962+
# find the right method for dispatching
953963
if cmd == 'help':
954964
docopt(__doc__, argv=['--help'])
955965
elif hasattr(cli, cmd):
956966
method = getattr(cli, cmd)
957-
return method(args)
958967
# magically call list if no subcommand provided
959968
elif hasattr(cli, cmd + '_list'):
960969
method = getattr(cli, cmd + '_list')
961-
return method(args)
962970
else:
963971
print 'Found no matching command'
964972
raise DocoptExit()
973+
# dispatch the CLI command
974+
try:
975+
method(args)
976+
except EnvironmentError as e:
977+
if e.message.startswith('Could not find deis remote'):
978+
print 'Could not find git remote for deis'
979+
raise DocoptExit()
980+
raise e
965981

966982

967983
if __name__ == '__main__':

0 commit comments

Comments
 (0)