Mistral bash completion script optimization

Make bash completion script generated automatically by using  'mistral
bash-completion' commands

Add a unit test for 'bash-completion' command.

Partially Implements: blueprint bash-completion-script-optimization
Change-Id: I97b15a1b9f41f98f5928f8bda4769957a37678d2
This commit is contained in:
LingxianKong 2015-05-18 11:09:25 +08:00
parent 1e79ff5471
commit ad4f2b34b7
2 changed files with 70 additions and 65 deletions

@ -0,0 +1,51 @@
# Copyright 2015 Huawei Technologies Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import six
import testtools
from mistralclient import shell
class TestCLIBashCompletionV2(testtools.TestCase):
def shell(self, argstr):
orig = (sys.stdout, sys.stderr)
clean_env = {}
_old_env, os.environ = os.environ, clean_env.copy()
try:
sys.stdout = six.moves.cStringIO()
sys.stderr = six.moves.cStringIO()
_shell = shell.MistralShell()
_shell.run(argstr.split())
except SystemExit:
exc_type, exc_value, exc_traceback = sys.exc_info()
self.assertEqual(exc_value.code, 0)
finally:
stdout = sys.stdout.getvalue()
stderr = sys.stderr.getvalue()
sys.stdout.close()
sys.stderr.close()
sys.stdout, sys.stderr = orig
os.environ = _old_env
return stdout, stderr
def test_bash_completion(self):
bash_completion, stderr = self.shell('bash-completion')
self.assertIn('bash-completion', bash_completion)
self.assertFalse(stderr)

@ -1,71 +1,25 @@
#!/bin/bash
_mistral_opts="" # lazy init
_mistral_flags="" # lazy init
_mistral_opts_exp="" # lazy init
_mistral()
{
declare -A SUBCOMMANDS
declare -A OPTS
local cur prev mbc cflags
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
OPTS["action-execution-get"]="-h --help -f --format -c --column --max-width --prefix"
OPTS["action-execution-get-input"]="-h --help"
OPTS["action-execution-get-output"]="-h --help"
OPTS["action-execution-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["action-execution-update"]="-h --help -f --format -c --column --max-width --prefix --state --output"
OPTS["action-create"]="-h --help -f --format -c --column --max-width --quote"
OPTS["action-delete"]="-h --help"
OPTS["action-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["action-get-definition"]="-h --help"
OPTS["action-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["action-update"]="-h --help -f --format -c --column --max-width --quote"
OPTS["complete"]="-h --help --name --shell"
OPTS["cron-trigger-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["cron-trigger-delete"]="-h --help"
OPTS["cron-trigger-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["cron-trigger-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["environment-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["environment-delete"]="-h --help"
OPTS["environment-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["environment-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["environment-update"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["execution-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["execution-delete"]="-h --help"
OPTS["execution-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["execution-get-input"]="-h --help"
OPTS["execution-get-output"]="-h --help"
OPTS["execution-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["execution-update"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["help"]="-h --help"
OPTS["task-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["task-get-result"]="-h --help"
OPTS["task-get-published"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["task-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["workbook-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["workbook-delete"]="-h --help"
OPTS["workbook-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["workbook-get-definition"]="-h --help"
OPTS["workbook-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["workbook-update"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["workbook-validate"]="-h --help -f --format -c --column --max-width --prefix"
OPTS["workflow-create"]="-h --help -f --format -c --column --max-width --quote"
OPTS["workflow-delete"]="-h --help"
OPTS["workflow-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
OPTS["workflow-get-definition"]="-h --help"
OPTS["workflow-list"]="-h --help -f --format -c --column --max-width --quote"
OPTS["workflow-update"]="-h --help -f --format -c --column --max-width --quote"
OPTS["workflow-validate"]="-h --help -f --format -c --column --max-width --prefix"
if [ "x$_mistral_opts" == "x" ] ; then
mbc="`mistral bash-completion`"
_mistral_opts="`echo "$mbc" | sed -e "s/\s-[a-z0-9_-]*//g" -e "s/\s\s*/ /g"`"
_mistral_flags="`echo " $mbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/\s\s*/ /g"`"
_mistral_opts_exp="`echo "$_mistral_opts" | sed -e "s/\s/|/g"`"
fi
COMMANDS="${!OPTS[*]}"
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ $cur =~ (\.|\~|\/).* ]] ; then
_filedir
elif [ $COMP_CWORD == "1" ] ; then
COMPREPLY=($(compgen -W "$COMMANDS" -- ${cur}))
elif [ $COMP_CWORD == "2" ] ; then
COMPREPLY=($(compgen -W "${OPTS[${prev}]}" -- ${cur}))
fi
return 0
if [[ " ${COMP_WORDS[@]} " =~ " "($_mistral_opts_exp)" " && "$prev" != "help" ]] ; then
COMPREPLY=($(compgen -W "${_mistral_flags}" -- ${cur}))
else
COMPREPLY=($(compgen -W "${_mistral_opts}" -- ${cur}))
fi
return 0
}
complete -F _mistral mistral