Skip to content

Commit

Permalink
Updated behaviours of add/edit commands, added a search command.
Browse files Browse the repository at this point in the history
* New behaviour of add/edit:
    NEW: storm add name host@server:port
    OLD: storm add name host server --port

    NEW: storm edit name host@server:port
    OLD: storm edit name host server --port

* Added a search command for searching host entries
    $ storm search vps
    Listing results for vps:
      vps -> [email protected]:22
  • Loading branch information
emre committed May 28, 2013
1 parent c32542c commit 85bb9d9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
3 changes: 3 additions & 0 deletions storm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def delete_all_entries(self):

return True

def search_host(self, search_string):
return self.ssh_config.search_host(search_string)

def get_options(self, host, user, port, id_file):
options = {
'hostname': host,
Expand Down
27 changes: 25 additions & 2 deletions storm/bin/storm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from manage import Manager
from storm import Storm
from storm.exceptions import StormValueError
from storm.ssh_uri_parser import parse

from termcolor import colored

Expand Down Expand Up @@ -33,11 +34,12 @@ def get_formatted_message(message, format_type):


@manager.command
def add(name, host, user, port=22, id_file=""):
def add(name, connection_uri, id_file=""):
"""
Adds a new entry to sshconfig.
"""
try:
user, host, port = parse(connection_uri)
result = storm_.add_entry(name, host, user, port, id_file)
return get_formatted_message('{0} added to your ssh config. you can connect it by typing "ssh {0}".'.format(
name
Expand All @@ -47,13 +49,15 @@ def add(name, host, user, port=22, id_file=""):


@manager.command
def edit(name, host, user, port=22, id_file=""):
def edit(name, connection_uri, id_file=""):
"""
Edits the related entry already defined in ssh config.
"""
try:
if ',' in name:
name = " ".join(name.split(","))

user, host, port = parse(connection_uri)
result = storm_.edit_entry(name, host, user, port, id_file)
return get_formatted_message(
'"{0}" updated successfully.'.format(
Expand Down Expand Up @@ -105,6 +109,25 @@ def list():
except Exception as error:
return get_formatted_message(str(error), 'error')

@manager.command
def search(search_text):
"""
searches entries by %LIKE%
"""
try:
results = storm_.search_host(search_text)
if len(results) == 0:
return ('no results found.')
message = 'Listing results for {0}:\n'.format(search_text)
message += "".join(results)
return message

except Exception as error:
import traceback
print traceback.print_exc()
return get_formatted_message(str(error), 'error')


@manager.command
def delete_all():
"""
Expand Down
4 changes: 4 additions & 0 deletions storm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@


class StormValueError(ValueError):
pass


class StormInvalidPortError(ValueError):
pass
22 changes: 22 additions & 0 deletions storm/ssh_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from os.path import exists
from paramiko.config import SSHConfig
from operator import itemgetter
import getpass

from exceptions import StormValueError

Expand Down Expand Up @@ -141,6 +142,27 @@ def update_host(self, host, options):

return self

def search_host(self, search_string):
results = []
for host_entry in self.config_data:
if host_entry.get("type") == 'entry':
searchable_information = host_entry.get("host")
for key, value in host_entry.get("options").items():
if isinstance(value, list):
value = " ".join(value)

searchable_information += " " + value

if search_string in searchable_information:
results.append(" {0} -> {1}@{2}:{3}\n".format(
host_entry.get("host"),
host_entry.get("options").get("user", getpass.getuser()),
host_entry.get("options").get("hostname"),
host_entry.get("options").get("port", 22),
))

return results

def delete_host(self, host):
found = 0
for index, host_entry in enumerate(self.config_data):
Expand Down
22 changes: 22 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import unittest
import os
import getpass

from storm import Storm
from storm.ssh_uri_parser import parse
from storm.exceptions import StormInvalidPortError


class StormTests(unittest.TestCase):
Expand Down Expand Up @@ -65,6 +68,25 @@ def test99_delete_all(self):
self.storm.delete_all_entries()
self.assertEqual(len(self.storm.ssh_config.config_data), 0)

def test_uri_parser(self):
user = getpass.getuser()
TEST_STRINGS = [
('[email protected]:22', ('root', 'emreyilmaz.me', 22)),
('emreyilmaz.me', (user, 'emreyilmaz.me', 22)),
('emreyilmaz.me:22', (user, 'emreyilmaz.me', 22)),
('[email protected]', ('root', 'emreyilmaz.me', 22))
]

for uri in TEST_STRINGS:
self.assertEqual(parse(uri[0]), uri[1])

# false strings
self.assertRaises(StormInvalidPortError, parse, '[email protected]:string-port')

def test_search_host(self):
results = self.storm.ssh_config.search_host("netsca")
self.assertEqual(len(results), 1)

def tearDown(self):
os.unlink('/tmp/ssh_config')

Expand Down

0 comments on commit 85bb9d9

Please sign in to comment.