Skip to content

Commit

Permalink
manage.py is no longer a dependency. updated the cli related stuff to…
Browse files Browse the repository at this point in the history
… use

@kadirpekel/komandr.
  • Loading branch information
emre committed Jul 18, 2013
1 parent 5a68a8e commit 16c60e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 51 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
manage.py==0.1b3
paramiko==1.10.1
termcolor
argparse
18 changes: 13 additions & 5 deletions storm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down
72 changes: 27 additions & 45 deletions storm/bin/storm
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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.
"""
Expand All @@ -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.
Expand Down Expand Up @@ -140,41 +128,35 @@ 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%
"""
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()

0 comments on commit 16c60e8

Please sign in to comment.