@@ -1228,35 +1228,53 @@ def keys_add(self, args):
12281228 if not path :
12291229 ssh_dir = os .path .expanduser ('~/.ssh' )
12301230 pubkeys = glob .glob (os .path .join (ssh_dir , '*.pub' ))
1231+ pubkeys_list = []
12311232 if not pubkeys :
12321233 print ('No SSH public keys found' )
12331234 return
12341235 print ('Found the following SSH public keys:' )
12351236 for i , k in enumerate (pubkeys ):
12361237 key = k .split (os .path .sep )[- 1 ]
1237- print ("{0}) {1}" .format (i + 1 , key ))
1238+ with open (k ) as f :
1239+ data = f .read ()
1240+ match = re .match (r'^(ssh-...) ([^ ]+) ?(.*)' , data )
1241+ if not match :
1242+ print ("Could not parse SSH public key {0}" .format (key ))
1243+ return
1244+ key_type , key_str , _key_comment = match .groups ()
1245+ pubkeys_list .append ([k , key , key_type , key_str , _key_comment ])
1246+ print ("{0}) {1} {2}" .format (i + 1 , key , _key_comment ))
1247+
12381248 inp = raw_input ('Which would you like to use with Deis? ' )
12391249 try :
1240- path = pubkeys [int (inp ) - 1 ]
1250+ selected_key = pubkeys_list [int (inp ) - 1 ]
1251+ path = selected_key [0 ]
12411252 except :
12421253 print ('Aborting' )
12431254 return
1244- key_id = path .split (os .path .sep )[- 1 ].replace ('.pub' , '' )
1245- with open (path ) as f :
1246- data = f .read ()
1247- match = re .match (r'^(ssh-...) ([^ ]+) ?(.*)' , data )
1248- if not match :
1249- print ('Could not parse public key material' )
1250- return
1251- key_type , key_str , _key_comment = match .groups ()
1252- body = {'id' : key_id , 'public' : "{0} {1}" .format (key_type , key_str )}
1253- sys .stdout .write ("Uploading {} to Deis... " .format (path ))
1254- sys .stdout .flush ()
1255- response = self ._dispatch ('post' , '/api/keys' , json .dumps (body ))
1256- if response .status_code == requests .codes .created : # @UndefinedVariable
1257- print ('done' )
1258- else :
1259- raise ResponseError (response )
1255+
1256+ if not selected_key [4 ]:
1257+ body = {
1258+ 'id' : selected_key [1 ].replace ('.pub' , '' ),
1259+ 'public' : "{0} {1}" .format (
1260+ selected_key [2 ], selected_key [3 ]
1261+ )
1262+ }
1263+ sys .stdout .write ("Uploading {} to Deis..." .format (selected_key [1 ]))
1264+ else :
1265+ body = {
1266+ 'id' : selected_key [4 ],
1267+ 'public' : "{0} {1}" .format (
1268+ selected_key [2 ], selected_key [3 ]
1269+ )
1270+ }
1271+ sys .stdout .write ("Uploading {} to Deis..." .format (selected_key [4 ]))
1272+ sys .stdout .flush ()
1273+ response = self ._dispatch ('post' , '/api/keys' , json .dumps (body ))
1274+ if response .status_code == requests .codes .created : # @UndefinedVariable
1275+ print ('done' )
1276+ else :
1277+ raise ResponseError (response )
12601278
12611279 def keys_list (self , args ):
12621280 """
0 commit comments