Add bash completion support for ironic cli
This commit adds the 'bash-completion' option to ironic cli which prints all sub-commands and options, and bash_completion scripts for bash auto completion. Change-Id: Ide5b29e262b4217046f124d86902cd0abfdab1b6 Closes-Bug: 1332408
This commit is contained in:
@@ -42,6 +42,16 @@ supplying an auth token using :option:`--ironic-url` and
|
||||
export IRONIC_URL=http://ironic.example.org:6385/
|
||||
export OS_AUTH_TOKEN=3bcc3d3a03f44e3d8377f9247b0ad155
|
||||
|
||||
Ironic cli supports bash completion. The command-line tool can automatically
|
||||
fill partially typed commands. To use this feature, source the below file to
|
||||
your terminal and then bash completion should work::
|
||||
|
||||
source tools/ironic.bash_completion
|
||||
|
||||
To avoid doing this everytime, add this to your ``.bashrc`` or copy the
|
||||
ironic.bash_completion file to the default bash completion scripts directory
|
||||
on your linux distribution.
|
||||
|
||||
OPTIONS
|
||||
=======
|
||||
|
||||
|
||||
@@ -195,6 +195,16 @@ class IronicShell(object):
|
||||
format="%(levelname)s %(message)s",
|
||||
level=logging.CRITICAL)
|
||||
|
||||
def _bash_completion(self):
|
||||
"""Prints all of the commands and options for bash-completion."""
|
||||
commands = set()
|
||||
options = set()
|
||||
for sc_str, sc in self.subcommands.items():
|
||||
commands.add(sc_str)
|
||||
for option in sc._optionals._option_string_actions.keys():
|
||||
options.add(option)
|
||||
print(' '.join(commands | options))
|
||||
|
||||
def main(self, argv):
|
||||
# Parse args once to find version
|
||||
parser = self.get_base_parser()
|
||||
@@ -211,6 +221,9 @@ class IronicShell(object):
|
||||
if options.help or not argv:
|
||||
self.do_help(options)
|
||||
return 0
|
||||
elif argv[0] == 'bash-completion':
|
||||
self._bash_completion()
|
||||
return 0
|
||||
|
||||
# Parse args again and call whatever callback was selected
|
||||
args = subcommand_parser.parse_args(argv)
|
||||
|
||||
@@ -93,3 +93,16 @@ class ShellTest(utils.BaseTestCase):
|
||||
def test_auth_param(self):
|
||||
self.make_env(exclude='OS_USERNAME')
|
||||
self.test_help()
|
||||
|
||||
def test_bash_completion(self):
|
||||
stdout = self.shell('bash-completion')
|
||||
# just check we have some output
|
||||
required = [
|
||||
'.*--driver_info',
|
||||
'.*--chassis_uuid',
|
||||
'.*help',
|
||||
'.*node-create',
|
||||
'.*chassis-create']
|
||||
for r in required:
|
||||
self.assertThat(stdout,
|
||||
matchers.MatchesRegex(r, self.re_options))
|
||||
|
||||
27
tools/ironic.bash_completion
Normal file
27
tools/ironic.bash_completion
Normal file
@@ -0,0 +1,27 @@
|
||||
_ironic_opts="" # lazy init
|
||||
_ironic_flags="" # lazy init
|
||||
_ironic_opts_exp="" # lazy init
|
||||
_ironic()
|
||||
{
|
||||
local cur prev nbc cflags
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
|
||||
if [ "x$_ironic_opts" == "x" ] ; then
|
||||
nbc="`ironic bash-completion | sed -e "s/ *-h */ /" -e "s/ *-i */ /"`"
|
||||
_ironic_opts="`echo "$nbc" | sed -e "s/--[a-z0-9_-]*//g" -e "s/ */ /g"`"
|
||||
_ironic_flags="`echo " $nbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/ */ /g"`"
|
||||
_ironic_opts_exp="`echo "$_ironic_opts" | tr ' ' '|'`"
|
||||
fi
|
||||
|
||||
if [[ " ${COMP_WORDS[@]} " =~ " "($_ironic_opts_exp)" " && "$prev" != "help" ]] ; then
|
||||
COMPLETION_CACHE=$HOME/.cache/python-ironicclient/*/*-cache
|
||||
cflags="$_ironic_flags "$(cat $COMPLETION_CACHE 2> /dev/null | tr '\n' ' ')
|
||||
COMPREPLY=($(compgen -W "${cflags}" -- ${cur}))
|
||||
else
|
||||
COMPREPLY=($(compgen -W "${_ironic_opts}" -- ${cur}))
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
complete -F _ironic ironic
|
||||
Reference in New Issue
Block a user