From 16c60e8ee874bcfaa008c764108bac100d56f9f8 Mon Sep 17 00:00:00 2001 From: emre Date: Thu, 18 Jul 2013 11:37:11 +0300 Subject: [PATCH] manage.py is no longer a dependency. updated the cli related stuff to use @kadirpekel/komandr. --- requirements.txt | 1 - storm/__init__.py | 18 ++++++++---- storm/bin/storm | 72 ++++++++++++++++++----------------------------- 3 files changed, 40 insertions(+), 51 deletions(-) diff --git a/requirements.txt b/requirements.txt index 3b581cb..33b1caa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -manage.py==0.1b3 paramiko==1.10.1 termcolor argparse diff --git a/storm/__init__.py b/storm/__init__.py index 614b73e..20b6d77 100644 --- a/storm/__init__.py +++ b/storm/__init__.py @@ -16,22 +16,22 @@ def __init__(self, ssh_config_file=None): self.ssh_config = ConfigParser(ssh_config_file) self.ssh_config.load() - def add_entry(self, name, host, user, port, id_file): + def add_entry(self, name, host, user, port, id_file, custom_options=[]): if self.is_host_in(name): raise StormValueError('{0} is already in your sshconfig. use storm edit command to modify.'.format(name)) - options = self.get_options(host, user, port, id_file) + options = self.get_options(host, user, port, id_file, custom_options) self.ssh_config.add_host(name, options) self.ssh_config.write_to_ssh_config() return True - def edit_entry(self, name, host, user, port, id_file): + def edit_entry(self, name, host, user, port, id_file, custom_options=[]): if not self.is_host_in(name): raise StormValueError('{0} doesn\'t exists in your sshconfig. use storm add command to add.'.format(name)) - options = self.get_options(host, user, port, id_file) + options = self.get_options(host, user, port, id_file, custom_options) self.ssh_config.update_host(name, options) self.ssh_config.write_to_ssh_config() @@ -68,7 +68,7 @@ def search_host(self, search_string): return formatted_results - def get_options(self, host, user, port, id_file): + def get_options(self, host, user, port, id_file, custom_options): options = { 'hostname': host, 'user': user, @@ -80,6 +80,14 @@ def get_options(self, host, user, port, id_file): 'identityfile': id_file, }) + if len(custom_options) > 0: + for custom_option in custom_options: + if '=' in custom_option: + key, value = custom_option.split("=")[0:2] + options.update({ + key: value, + }) + return options def is_host_in(self, host): diff --git a/storm/bin/storm b/storm/bin/storm index 71f0b7a..733befc 100755 --- a/storm/bin/storm +++ b/storm/bin/storm @@ -1,23 +1,15 @@ #!/usr/bin/python -try: - from manage import Manager -except ImportError: - # since manage.py library changed its api, try to import with the old syntax. - try: - from manager import Manager - except ImportError: - raise Exception('manage.py library is not installed.') +import getpass from storm import Storm from storm import __version__ from storm.exceptions import StormValueError from storm.ssh_uri_parser import parse +from storm.kommandr import * from termcolor import colored -import getpass -manager = Manager() storm_ = Storm() default_user = getpass.getuser() @@ -45,16 +37,15 @@ def get_formatted_message(message, format_type): return all_message+message -@manager.command +@command('version') def version(): """ prints the working storm(ssh) version. """ - return __version__ - + print __version__ -@manager.command -def add(name, connection_uri, id_file=""): +@command('add') +def add(name, connection_uri, id_file="", o=[]): """ Adds a new entry to sshconfig. """ @@ -63,18 +54,17 @@ def add(name, connection_uri, id_file=""): # validate name if '@' in name: raise StormValueError('invalid value: "@" cannot be used in name.') - 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( + result = storm_.add_entry(name, host, user, port, id_file, o) + print get_formatted_message('{0} added to your ssh config. you can connect it by typing "ssh {0}".'.format( name ), 'success') except StormValueError as error: - return get_formatted_message(error, 'error') + print get_formatted_message(error, 'error') -@manager.command -def edit(name, connection_uri, id_file=""): +@command('edit') +def edit(name, connection_uri, id_file="", o=[]): """ Edits the related entry already defined in ssh config. """ @@ -84,27 +74,25 @@ def edit(name, connection_uri, id_file=""): user, host, port = parse(connection_uri) result = storm_.edit_entry(name, host, user, port, id_file) - return get_formatted_message( + print get_formatted_message( '"{0}" updated successfully.'.format( name ), 'success') except StormValueError as error: - return get_formatted_message(error, 'error') - + print get_formatted_message(error, 'error') -@manager.command +@command('delete') def delete(name): """ Deletes a single host. """ try: result = storm_.delete_entry(name) - return get_formatted_message('hostname "{0}" deleted successfully.'.format(name), 'success') + print get_formatted_message('hostname "{0}" deleted successfully.'.format(name), 'success') except StormValueError as error: - return get_formatted_message(error, 'error') - + print get_formatted_message(error, 'error') -@manager.command +@command('list') def list(): """ Lists all hosts from ssh config. @@ -140,12 +128,11 @@ def list(): result_stack = result_stack[0:-2] + "\n" result += result_stack - return result + print result except Exception as error: - return get_formatted_message(str(error), 'error') + print get_formatted_message(str(error), 'error') - -@manager.command +@command('search') def search(search_text): """ searches entries by %LIKE% @@ -153,28 +140,23 @@ def search(search_text): try: results = storm_.search_host(search_text) if len(results) == 0: - return ('no results found.') + print ('no results found.') message = 'Listing results for {0}:\n'.format(search_text) message += "".join(results) - return message - + print message except Exception as error: - return get_formatted_message(str(error), 'error') - + print get_formatted_message(str(error), 'error') -@manager.command +@command('delete_all') def delete_all(): """ Deletes all hosts from ssh config. """ try: storm_.delete_all_entries() - return get_formatted_message('all entries deleted.', 'success') + print get_formatted_message('all entries deleted.', 'success') except Exception as error: - return get_formatted_message(str(error), 'error') - - -manager.main() - + print get_formatted_message(str(error), 'error') +main()