Skip to content

Commit 48310a9

Browse files
committed
Merge pull request #488 from nathansamson/nathan/cli-improvements
Looks good, thanks very much!
2 parents 2472f02 + 93f8210 commit 48310a9

1 file changed

Lines changed: 57 additions & 41 deletions

File tree

client/deis.py

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,8 @@ def config(self, args):
784784
785785
Use `deis help [command]` to learn more
786786
"""
787+
sys.argv[1] = 'config:list'
788+
args = docopt(self.config_list.__doc__)
787789
return self.config_list(args)
788790

789791
def config_list(self, args):
@@ -886,17 +888,20 @@ def containers(self, args):
886888
887889
Use `deis help [command]` to learn more
888890
"""
891+
sys.argv[1] = 'containers:list'
892+
args = docopt(self.containers_list.__doc__)
889893
return self.containers_list(args)
890894

891-
def containers_list(self, args):
895+
def containers_list(self, args, app=None):
892896
"""
893897
List containers servicing an application
894898
895899
Usage: deis containers:list [--app=<app>]
896900
"""
897-
app = args.get('--app')
898901
if not app:
899-
app = self._session.get_app()
902+
app = args.get('--app')
903+
if not app:
904+
app = self._session.get_app()
900905
response = self._dispatch('get',
901906
"/api/apps/{}/containers".format(app))
902907
if response.status_code != requests.codes.ok: # @UndefinedVariable
@@ -947,7 +952,7 @@ def containers_scale(self, args):
947952
progress.join()
948953
if response.status_code == requests.codes.ok: # @UndefinedVariable
949954
print('done in {}s\n'.format(int(time.time() - before)))
950-
self.containers_list({})
955+
self.containers_list({}, app)
951956
else:
952957
raise ResponseError(response)
953958

@@ -1273,60 +1278,69 @@ def keys_add(self, args):
12731278
Usage: deis keys:add [<key>]
12741279
"""
12751280

1276-
Key = namedtuple('Key', 'path name type str comment')
1277-
1278-
def parse_key(path):
1279-
"""Parse an SSH public key path into a Key namedtuple."""
1280-
name = path.split(os.path.sep)[-1]
1281-
with open(path) as f:
1282-
data = f.read()
1283-
match = re.match(r'^(ssh-...) ([^ ]+) ?(.*)', data)
1284-
if not match:
1285-
print("Could not parse SSH public key {0}".format(name))
1286-
return
1287-
key_type, key_str, key_comment = match.groups()
1288-
return Key(path, name, key_type, key_str, key_comment)
1289-
12901281
path = args.get('<key>')
12911282
if not path:
1292-
# find public keys and prompt the user to pick one
1293-
ssh_dir = os.path.expanduser('~/.ssh')
1294-
pubkey_paths = glob.glob(os.path.join(ssh_dir, '*.pub'))
1295-
if not pubkey_paths:
1296-
print('No SSH public keys found')
1297-
return
1298-
pubkeys_list = [parse_key(k) for k in pubkey_paths]
1299-
print('Found the following SSH public keys:')
1300-
for i, key_ in enumerate(pubkeys_list):
1301-
print("{}) {} {}".format(i + 1, key_.name, key_.comment))
1302-
inp = raw_input('Which would you like to use with Deis? ')
1303-
try:
1304-
selected_key = pubkeys_list[int(inp) - 1]
1305-
except:
1306-
print('Aborting')
1307-
return
1283+
selected_key = self._ask_pubkey_interactively()
13081284
else:
13091285
# check the specified key format
1310-
selected_key = parse_key(path)
1286+
selected_key = self._parse_key(path)
13111287
if not selected_key:
13121288
return
13131289
# Upload the key to Deis
1314-
if selected_key.comment:
1315-
key_id = selected_key.comment
1316-
else:
1317-
key_id = selected_key.name.replace('.pub', '')
13181290
body = {
1319-
'id': key_id,
1291+
'id': selected_key.id,
13201292
'public': "{} {}".format(selected_key.type, selected_key.str)
13211293
}
1322-
sys.stdout.write("Uploading {} to Deis...".format(key_id))
1294+
sys.stdout.write("Uploading {} to Deis...".format(selected_key.id))
13231295
sys.stdout.flush()
13241296
response = self._dispatch('post', '/api/keys', json.dumps(body))
13251297
if response.status_code == requests.codes.created: # @UndefinedVariable
13261298
print('done')
13271299
else:
13281300
raise ResponseError(response)
13291301

1302+
def _parse_key(self, path):
1303+
"""Parse an SSH public key path into a Key namedtuple."""
1304+
Key = namedtuple('Key', 'path name type str comment id')
1305+
1306+
name = path.split(os.path.sep)[-1]
1307+
with open(path) as f:
1308+
data = f.read()
1309+
match = re.match(r'^(ssh-...) ([^ ]+) ?(.*)', data)
1310+
if not match:
1311+
print("Could not parse SSH public key {0}".format(name))
1312+
return
1313+
key_type, key_str, key_comment = match.groups()
1314+
if key_comment:
1315+
key_id = key_comment
1316+
else:
1317+
key_id = name.replace('.pub', '')
1318+
return Key(path, name, key_type, key_str, key_comment, key_id)
1319+
1320+
def _ask_pubkey_interactively(self):
1321+
# find public keys and prompt the user to pick one
1322+
ssh_dir = os.path.expanduser('~/.ssh')
1323+
pubkey_paths = glob.glob(os.path.join(ssh_dir, '*.pub'))
1324+
if not pubkey_paths:
1325+
print('No SSH public keys found')
1326+
return
1327+
pubkeys_list = [self._parse_key(k) for k in pubkey_paths]
1328+
print('Found the following SSH public keys:')
1329+
for i, key_ in enumerate(pubkeys_list):
1330+
print("{}) {} {}".format(i + 1, key_.name, key_.comment))
1331+
print("0) Enter path to pubfile (or use keys:add <key_path>) ")
1332+
inp = raw_input('Which would you like to use with Deis? ')
1333+
try:
1334+
if int(inp) != 0:
1335+
selected_key = pubkeys_list[int(inp) - 1]
1336+
else:
1337+
selected_key_path = raw_input('Enter the path to the pubkey file: ')
1338+
selected_key = self._parse_key(os.path.expanduser(selected_key_path))
1339+
except:
1340+
print('Aborting')
1341+
return
1342+
return selected_key
1343+
13301344
def keys_list(self, args):
13311345
"""
13321346
List SSH keys for the logged in user
@@ -1728,6 +1742,8 @@ def perms(self, args):
17281742
Use `deis help perms:[command]` to learn more
17291743
"""
17301744
# perms:transfer transfer ownership of an app or formation
1745+
sys.argv[1] = 'perms:list'
1746+
args = docopt(self.perms_list.__doc__)
17311747
return self.perms_list(args)
17321748

17331749
def perms_list(self, args):

0 commit comments

Comments
 (0)