Skip to content

Commit 93f8210

Browse files
committed
Trick flake into decreasing keys_add CC
1 parent 9d23b0f commit 93f8210

1 file changed

Lines changed: 46 additions & 42 deletions

File tree

client/deis.py

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,65 +1278,69 @@ def keys_add(self, args):
12781278
Usage: deis keys:add [<key>]
12791279
"""
12801280

1281-
Key = namedtuple('Key', 'path name type str comment')
1282-
1283-
def parse_key(path):
1284-
"""Parse an SSH public key path into a Key namedtuple."""
1285-
name = path.split(os.path.sep)[-1]
1286-
with open(path) as f:
1287-
data = f.read()
1288-
match = re.match(r'^(ssh-...) ([^ ]+) ?(.*)', data)
1289-
if not match:
1290-
print("Could not parse SSH public key {0}".format(name))
1291-
return
1292-
key_type, key_str, key_comment = match.groups()
1293-
return Key(path, name, key_type, key_str, key_comment)
1294-
12951281
path = args.get('<key>')
12961282
if not path:
1297-
# find public keys and prompt the user to pick one
1298-
ssh_dir = os.path.expanduser('~/.ssh')
1299-
pubkey_paths = glob.glob(os.path.join(ssh_dir, '*.pub'))
1300-
if not pubkey_paths:
1301-
print('No SSH public keys found')
1302-
return
1303-
pubkeys_list = [parse_key(k) for k in pubkey_paths]
1304-
print('Found the following SSH public keys:')
1305-
for i, key_ in enumerate(pubkeys_list):
1306-
print("{}) {} {}".format(i + 1, key_.name, key_.comment))
1307-
print("0) Enter path to pubfile (or use keys:add <key_path>) ")
1308-
inp = raw_input('Which would you like to use with Deis? ')
1309-
try:
1310-
if int(inp) != 0:
1311-
selected_key = pubkeys_list[int(inp) - 1]
1312-
else:
1313-
selected_key_path = raw_input('Enter the path to the pubkey file: ')
1314-
selected_key = parse_key(os.path.expanduser(selected_key_path))
1315-
except:
1316-
print('Aborting')
1317-
return
1283+
selected_key = self._ask_pubkey_interactively()
13181284
else:
13191285
# check the specified key format
1320-
selected_key = parse_key(path)
1286+
selected_key = self._parse_key(path)
13211287
if not selected_key:
13221288
return
13231289
# Upload the key to Deis
1324-
if selected_key.comment:
1325-
key_id = selected_key.comment
1326-
else:
1327-
key_id = selected_key.name.replace('.pub', '')
13281290
body = {
1329-
'id': key_id,
1291+
'id': selected_key.id,
13301292
'public': "{} {}".format(selected_key.type, selected_key.str)
13311293
}
1332-
sys.stdout.write("Uploading {} to Deis...".format(key_id))
1294+
sys.stdout.write("Uploading {} to Deis...".format(selected_key.id))
13331295
sys.stdout.flush()
13341296
response = self._dispatch('post', '/api/keys', json.dumps(body))
13351297
if response.status_code == requests.codes.created: # @UndefinedVariable
13361298
print('done')
13371299
else:
13381300
raise ResponseError(response)
13391301

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+
13401344
def keys_list(self, args):
13411345
"""
13421346
List SSH keys for the logged in user

0 commit comments

Comments
 (0)