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
This commit is contained in:
parent
d8b499bd6d
commit
ea78b2e4fc
|
@ -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()
|
||||
|
|
|
@ -11,11 +11,11 @@ from keystone.common import cfg
|
|||
gettext.install('keystone', unicode=1)
|
||||
|
||||
|
||||
class Config(cfg.CommonConfigOpts):
|
||||
class ConfigMixin(object):
|
||||
def __call__(self, config_files=None, *args, **kw):
|
||||
if config_files is not None:
|
||||
self._opts['config_file']['opt'].default = config_files
|
||||
return super(Config, self).__call__(*args, **kw)
|
||||
return super(ConfigMixin, self).__call__(*args, **kw)
|
||||
|
||||
def __getitem__(self, key, default=None):
|
||||
return getattr(self, key, default)
|
||||
|
@ -27,6 +27,21 @@ class Config(cfg.CommonConfigOpts):
|
|||
for k in self._opts:
|
||||
yield (k, getattr(self, k))
|
||||
|
||||
def print_help(self):
|
||||
self._oparser.print_help()
|
||||
|
||||
def set_usage(self, usage):
|
||||
self.usage = usage
|
||||
self._oparser.usage = usage
|
||||
|
||||
|
||||
class Config(ConfigMixin, cfg.ConfigOpts):
|
||||
pass
|
||||
|
||||
|
||||
class CommonConfig(ConfigMixin, cfg.CommonConfigOpts):
|
||||
pass
|
||||
|
||||
|
||||
def setup_logging(conf):
|
||||
"""
|
||||
|
@ -76,33 +91,37 @@ def setup_logging(conf):
|
|||
|
||||
|
||||
def register_str(*args, **kw):
|
||||
group = _ensure_group(kw)
|
||||
return CONF.register_opt(cfg.StrOpt(*args, **kw), group=group)
|
||||
conf = kw.pop('conf', CONF)
|
||||
group = _ensure_group(kw, conf)
|
||||
return conf.register_opt(cfg.StrOpt(*args, **kw), group=group)
|
||||
|
||||
|
||||
def register_cli_str(*args, **kw):
|
||||
group = _ensure_group(kw)
|
||||
return CONF.register_cli_opt(cfg.StrOpt(*args, **kw), group=group)
|
||||
conf = kw.pop('conf', CONF)
|
||||
group = _ensure_group(kw, conf)
|
||||
return conf.register_cli_opt(cfg.StrOpt(*args, **kw), group=group)
|
||||
|
||||
|
||||
def register_bool(*args, **kw):
|
||||
group = _ensure_group(kw)
|
||||
return CONF.register_opt(cfg.BoolOpt(*args, **kw), group=group)
|
||||
conf = kw.pop('conf', CONF)
|
||||
group = _ensure_group(kw, conf)
|
||||
return conf.register_opt(cfg.BoolOpt(*args, **kw), group=group)
|
||||
|
||||
|
||||
def register_cli_bool(*args, **kw):
|
||||
group = _ensure_group(kw)
|
||||
return CONF.register_cli_opt(cfg.BoolOpt(*args, **kw), group=group)
|
||||
conf = kw.pop('conf', CONF)
|
||||
group = _ensure_group(kw, conf)
|
||||
return conf.register_cli_opt(cfg.BoolOpt(*args, **kw), group=group)
|
||||
|
||||
|
||||
def _ensure_group(kw):
|
||||
def _ensure_group(kw, conf):
|
||||
group = kw.pop('group', None)
|
||||
if group:
|
||||
CONF.register_group(cfg.OptGroup(name=group))
|
||||
conf.register_group(cfg.OptGroup(name=group))
|
||||
return group
|
||||
|
||||
|
||||
CONF = Config(project='keystone')
|
||||
CONF = CommonConfig(project='keystone')
|
||||
|
||||
|
||||
register_str('admin_token', default='ADMIN')
|
||||
|
|
Loading…
Reference in New Issue