summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authortermie <github@anarkystic.com>2012-01-19 15:54:14 -0800
committertermie <github@anarkystic.com>2012-01-19 20:10:17 -0800
commitea78b2e4fcdefd13e35cb1eb28559f18a9cc6f1c (patch)
tree9e0b8e91147de7863cd6c17022456e3fd80bb917 /bin
parentd8b499bd6dc1fe87c824b9a03dcbb6f5bd574b12 (diff)
some cli improvements
prints available commands and config values when no arguments are given prints available subcommands when command is given but no subocmmand is
Diffstat (limited to 'bin')
-rwxr-xr-xbin/keystone-manage75
1 files changed, 67 insertions, 8 deletions
diff --git a/bin/keystone-manage b/bin/keystone-manage
index d290c8cd..ae83c1e7 100755
--- a/bin/keystone-manage
+++ b/bin/keystone-manage
@@ -2,6 +2,7 @@
import os
import sys
+import textwrap
import cli.app
import cli.log
@@ -22,16 +23,24 @@ from keystone import config
from keystone.common import utils
-CONF = config.CONF
+CONF = config.Config(usage='%prog COMMAND [key1=value1 key2=value2 ...]')
config.register_cli_str('endpoint',
default='http://localhost:$admin_port/v2.0',
- group='ks')
+ #group='ks',
+ conf=CONF)
config.register_cli_str('token',
default='$admin_token',
- group='ks')
+ #group='ks',
+ help='asdasd',
+ conf=CONF)
config.register_cli_bool('id-only',
default=False,
- group='ks')
+ #group='ks',
+ conf=CONF)
+config.register_cli_str('admin-port',
+ conf=CONF)
+config.register_cli_str('admin-token',
+ conf=CONF)
class BaseApp(cli.log.LoggingApp):
@@ -59,6 +68,8 @@ class BaseApp(cli.log.LoggingApp):
class DbSync(BaseApp):
+ """Sync the database."""
+
name = 'db_sync'
def __init__(self, *args, **kw):
@@ -77,12 +88,13 @@ class ClientCommand(BaseApp):
def __init__(self, *args, **kw):
super(ClientCommand, self).__init__(*args, **kw)
if not self.ACTION_MAP:
- self.ACTION_MAP = {}
- self.add_param('action')
+ self.ACTION_MAP = {'help': 'help'}
+ self.add_param('action', nargs='?', default='help')
self.add_param('keyvalues', nargs='*')
- self.client = kc.Client(CONF.ks.endpoint, token=CONF.ks.token)
+ self.client = kc.Client(CONF.endpoint, token=CONF.token)
self.handle = getattr(self.client, '%ss' % self.__class__.__name__.lower())
self._build_action_map()
+ self.usage = "foo"
def _build_action_map(self):
actions = {}
@@ -94,6 +106,10 @@ class ClientCommand(BaseApp):
def main(self):
"""Given some keyvalues create the appropriate data in Keystone."""
action_name = self.ACTION_MAP[self.params.action]
+ if action_name == 'help':
+ self.print_help()
+ sys.exit(1)
+
kv = self._parse_keyvalues(self.params.keyvalues)
resp = getattr(self.handle, action_name)(**kv)
if CONF.ks.id_only and getattr(resp, 'id'):
@@ -101,24 +117,48 @@ class ClientCommand(BaseApp):
return
print resp
+ def print_help(self):
+ CONF.set_usage(CONF.usage.replace(
+ 'COMMAND', '%s SUBCOMMAND' % self.__class__.__name__.lower()))
+ CONF.print_help()
+
+ methods = self._get_methods()
+ print_commands(methods)
+
+ def _get_methods(self):
+ o = {}
+ for k in dir(self.handle):
+ if k.startswith('_'):
+ continue
+ if k in ('find', 'findall', 'api', 'resource_class'):
+ continue
+ o[k] = getattr(self.handle, k)
+ return o
+
class Role(ClientCommand):
+ """Role CRUD functions."""
pass
class Service(ClientCommand):
+ """Service CRUD functions."""
pass
class Token(ClientCommand):
+ """Token CRUD functions."""
pass
class Tenant(ClientCommand):
+ """Tenant CRUD functions."""
pass
class User(ClientCommand):
+ """User CRUD functions."""
+
pass
@@ -131,6 +171,21 @@ CMDS = {'db_sync': DbSync,
}
+def print_commands(cmds):
+ print
+ print "Available commands:"
+ o = []
+ max_length = max([len(k) for k in cmds]) + 2
+ for k, cmd in sorted(cmds.iteritems()):
+ initial_indent = '%s%s: ' % (' ' * (max_length - len(k)), k)
+ tw = textwrap.TextWrapper(initial_indent=initial_indent,
+ subsequent_indent=' ' * (max_length + 2),
+ width=80)
+ o.extend(tw.wrap(
+ (cmd.__doc__ and cmd.__doc__ or 'no docs').strip().split('\n')[0]))
+ print '\n'.join(o)
+
+
def main(argv=None):
if argv is None:
argv = sys.argv
@@ -141,8 +196,12 @@ def main(argv=None):
config_files = None
if os.path.exists(dev_conf):
config_files = [dev_conf]
-
args = CONF(config_files=config_files, args=argv)
+ if len(args) < 2:
+ CONF.print_help()
+ print_commands(CMDS)
+ sys.exit(1)
+
cmd = args[1]
if cmd in CMDS:
CMDS[cmd](argv=(args[:1] + args[2:])).run()