Skip to content

Commit ad4f2b3

Browse files
author
LingxianKong
committed
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
1 parent 1e79ff5 commit ad4f2b3

File tree

2 files changed

+70
-65
lines changed

2 files changed

+70
-65
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2015 Huawei Technologies Co., Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import sys
17+
18+
import six
19+
import testtools
20+
21+
from mistralclient import shell
22+
23+
24+
class TestCLIBashCompletionV2(testtools.TestCase):
25+
26+
def shell(self, argstr):
27+
orig = (sys.stdout, sys.stderr)
28+
clean_env = {}
29+
_old_env, os.environ = os.environ, clean_env.copy()
30+
try:
31+
sys.stdout = six.moves.cStringIO()
32+
sys.stderr = six.moves.cStringIO()
33+
_shell = shell.MistralShell()
34+
_shell.run(argstr.split())
35+
except SystemExit:
36+
exc_type, exc_value, exc_traceback = sys.exc_info()
37+
self.assertEqual(exc_value.code, 0)
38+
finally:
39+
stdout = sys.stdout.getvalue()
40+
stderr = sys.stderr.getvalue()
41+
sys.stdout.close()
42+
sys.stderr.close()
43+
sys.stdout, sys.stderr = orig
44+
os.environ = _old_env
45+
return stdout, stderr
46+
47+
def test_bash_completion(self):
48+
bash_completion, stderr = self.shell('bash-completion')
49+
50+
self.assertIn('bash-completion', bash_completion)
51+
self.assertFalse(stderr)

tools/mistral.bash_completion

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,25 @@
1-
#!/bin/bash
2-
1+
_mistral_opts="" # lazy init
2+
_mistral_flags="" # lazy init
3+
_mistral_opts_exp="" # lazy init
34
_mistral()
45
{
5-
declare -A SUBCOMMANDS
6-
declare -A OPTS
7-
8-
OPTS["action-execution-get"]="-h --help -f --format -c --column --max-width --prefix"
9-
OPTS["action-execution-get-input"]="-h --help"
10-
OPTS["action-execution-get-output"]="-h --help"
11-
OPTS["action-execution-list"]="-h --help -f --format -c --column --max-width --quote"
12-
OPTS["action-execution-update"]="-h --help -f --format -c --column --max-width --prefix --state --output"
13-
OPTS["action-create"]="-h --help -f --format -c --column --max-width --quote"
14-
OPTS["action-delete"]="-h --help"
15-
OPTS["action-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
16-
OPTS["action-get-definition"]="-h --help"
17-
OPTS["action-list"]="-h --help -f --format -c --column --max-width --quote"
18-
OPTS["action-update"]="-h --help -f --format -c --column --max-width --quote"
19-
OPTS["complete"]="-h --help --name --shell"
20-
OPTS["cron-trigger-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
21-
OPTS["cron-trigger-delete"]="-h --help"
22-
OPTS["cron-trigger-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
23-
OPTS["cron-trigger-list"]="-h --help -f --format -c --column --max-width --quote"
24-
OPTS["environment-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
25-
OPTS["environment-delete"]="-h --help"
26-
OPTS["environment-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
27-
OPTS["environment-list"]="-h --help -f --format -c --column --max-width --quote"
28-
OPTS["environment-update"]="-h --help -f --format -c --column --max-width --variable --prefix"
29-
OPTS["execution-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
30-
OPTS["execution-delete"]="-h --help"
31-
OPTS["execution-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
32-
OPTS["execution-get-input"]="-h --help"
33-
OPTS["execution-get-output"]="-h --help"
34-
OPTS["execution-list"]="-h --help -f --format -c --column --max-width --quote"
35-
OPTS["execution-update"]="-h --help -f --format -c --column --max-width --variable --prefix"
36-
OPTS["help"]="-h --help"
37-
OPTS["task-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
38-
OPTS["task-get-result"]="-h --help"
39-
OPTS["task-get-published"]="-h --help -f --format -c --column --max-width --variable --prefix"
40-
OPTS["task-list"]="-h --help -f --format -c --column --max-width --quote"
41-
OPTS["workbook-create"]="-h --help -f --format -c --column --max-width --variable --prefix"
42-
OPTS["workbook-delete"]="-h --help"
43-
OPTS["workbook-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
44-
OPTS["workbook-get-definition"]="-h --help"
45-
OPTS["workbook-list"]="-h --help -f --format -c --column --max-width --quote"
46-
OPTS["workbook-update"]="-h --help -f --format -c --column --max-width --variable --prefix"
47-
OPTS["workbook-validate"]="-h --help -f --format -c --column --max-width --prefix"
48-
OPTS["workflow-create"]="-h --help -f --format -c --column --max-width --quote"
49-
OPTS["workflow-delete"]="-h --help"
50-
OPTS["workflow-get"]="-h --help -f --format -c --column --max-width --variable --prefix"
51-
OPTS["workflow-get-definition"]="-h --help"
52-
OPTS["workflow-list"]="-h --help -f --format -c --column --max-width --quote"
53-
OPTS["workflow-update"]="-h --help -f --format -c --column --max-width --quote"
54-
OPTS["workflow-validate"]="-h --help -f --format -c --column --max-width --prefix"
55-
56-
COMMANDS="${!OPTS[*]}"
57-
COMPREPLY=()
6+
local cur prev mbc cflags
7+
COMPREPLY=()
8+
cur="${COMP_WORDS[COMP_CWORD]}"
9+
prev="${COMP_WORDS[COMP_CWORD-1]}"
5810

59-
local cur="${COMP_WORDS[COMP_CWORD]}"
60-
local prev="${COMP_WORDS[COMP_CWORD-1]}"
11+
if [ "x$_mistral_opts" == "x" ] ; then
12+
mbc="`mistral bash-completion`"
13+
_mistral_opts="`echo "$mbc" | sed -e "s/\s-[a-z0-9_-]*//g" -e "s/\s\s*/ /g"`"
14+
_mistral_flags="`echo " $mbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/\s\s*/ /g"`"
15+
_mistral_opts_exp="`echo "$_mistral_opts" | sed -e "s/\s/|/g"`"
16+
fi
6117

62-
if [[ $cur =~ (\.|\~|\/).* ]] ; then
63-
_filedir
64-
elif [ $COMP_CWORD == "1" ] ; then
65-
COMPREPLY=($(compgen -W "$COMMANDS" -- ${cur}))
66-
elif [ $COMP_CWORD == "2" ] ; then
67-
COMPREPLY=($(compgen -W "${OPTS[${prev}]}" -- ${cur}))
68-
fi
69-
return 0
18+
if [[ " ${COMP_WORDS[@]} " =~ " "($_mistral_opts_exp)" " && "$prev" != "help" ]] ; then
19+
COMPREPLY=($(compgen -W "${_mistral_flags}" -- ${cur}))
20+
else
21+
COMPREPLY=($(compgen -W "${_mistral_opts}" -- ${cur}))
22+
fi
23+
return 0
7024
}
7125
complete -F _mistral mistral

0 commit comments

Comments
 (0)