Skip to content

Commit 57bf05e

Browse files
authored
Merge pull request #216 from hpe-container-platform-community/cli_k8sworker
autocompletion improvements
2 parents 039288c + 9d7ac29 commit 57bf05e

File tree

3 files changed

+104
-39
lines changed

3 files changed

+104
-39
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
],
2727
"python.linting.enabled": true,
2828
"python.formatting.provider": "black",
29-
"python.pythonPath": "/bin/python3",
29+
"python.formatting.blackPath": "/bin/black",
30+
"python.pythonPath": "/usr/bin/python3.5",
3031
"python.envFile": "${workspaceFolder}/gitpod.env",
3132
"editor.formatOnSave": true,
3233
"files.insertFinalNewline": true,
3334
"git.alwaysSignOff": true,
3435
"autoDocstring.docstringFormat": "numpy"
35-
}
36+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/hpe-container-platform-community/hpecp-python-library)
1111
[![Good first issues open](https://img.shields.io/github/issues/hpe-container-platform-community/hpecp-python-library/good%20first%20issue.svg?label=good%20first%20issue)](https://github.com/hpe-container-platform-community/hpecp-python-library/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22)
12-
12+
1313

1414

1515
----

bin/cli.py

Lines changed: 100 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,36 +1647,120 @@ def bash(self,):
16471647
cur=${COMP_WORDS[COMP_CWORD]}
16481648
prev=${COMP_WORDS[COMP_CWORD-1]}
16491649
1650-
COMP_WORDS_AS_STRING=$(IFS=. ; echo "${COMP_WORDS[*]}")
1650+
MODULE=${COMP_WORDS[1]}
16511651
1652-
# if the last parameter was --*file
1653-
if echo "${prev}" | grep -q '\-\-.*file$'
1654-
then
1655-
_filedir;
1656-
return
1657-
fi
1652+
COMP_WORDS_AS_STRING=$(IFS=. ; echo "${COMP_WORDS[*]}")
16581653
1654+
# if last input was > for redirecting to a file
1655+
# perform file and directory autocompletion
16591656
if echo "${prev}" | grep -q '>'
16601657
then
16611658
_filedir;
16621659
return
16631660
fi
16641661
1662+
# from: https://stackoverflow.com/a/58221008/1033422
1663+
1664+
declare -A MODULE_COLUMNS=(
1665+
{% for module_name in modules %}
1666+
{% set column_names = " ".join(columns[module_name]) %}
1667+
['{{module_name}}']="{{column_names}}"
1668+
{% endfor %}
1669+
)
1670+
16651671
{% raw %}
1666-
for (( idx=${#COMP_WORDS[@]}-1 ; idx>=0 ; idx-- )) ; do
1667-
item="${COMP_WORDS[idx]}"
1668-
if [[ "${item:0:2}" == "--" ]]; then
1669-
if [[ "${item}" == "--columns" ]]
1672+
# list has uniform behaviour as it is implemented in BaseProxy
1673+
if [[ "${COMP_WORDS[2]}" == "list" ]];
1674+
then
1675+
1676+
# if 'list' was the last word
1677+
if [[ "${prev}" == "list" ]];
1678+
then
1679+
COMPREPLY=( $(compgen -W "--columns --query" -- $cur) )
1680+
return
1681+
fi
1682+
1683+
# FIXME: https://unix.stackexchange.com/questions/124539/bash-completion-for-comma-separated-values
1684+
1685+
# '--columns' was the last word and user is entering column names
1686+
if [[ "${COMP_WORDS[3]}" == "--columns"* && ${#COMP_WORDS[@]} -le 5 ]];
1687+
then
1688+
declare -a COLUMNS=(${MODULE_COLUMNS[$MODULE]})
1689+
1690+
local realcur prefix
1691+
realcur=${cur##*,} # everything after the last comma, e.g. a,b,c,d -> d
1692+
prefix=${cur%,*} # everything before the lat comma, e.g. a,b,c,d -> a,b,c
1693+
1694+
if [[ "$cur" == *,* ]];
1695+
then
1696+
IFS=',' ENTERED_COLUMNS_LIST=($prefix)
1697+
unset IFS
1698+
else
1699+
IFS=',' ENTERED_COLUMNS_LIST=($prev)
1700+
unset IFS
1701+
fi
1702+
1703+
for COLUMN in ${COLUMNS[@]}; do
1704+
for ENTERED_COLUMN in ${ENTERED_COLUMNS_LIST[@]}; do
1705+
if [[ "${ENTERED_COLUMN}" == "${COLUMN}" ]]
1706+
then
1707+
# remove columns already entered by user
1708+
COLUMNS=(${COLUMNS[*]//$ENTERED_COLUMN/})
1709+
fi
1710+
done
1711+
done
1712+
1713+
if [[ "$cur" == *,* ]];
16701714
then
1671-
LAST_PARAM_IS_COLUMNS=1
1715+
COMPREPLY=( $(compgen -W "${COLUMNS[*]}" -P "${prefix}," -S "," -- ${realcur}) )
1716+
compopt -o nospace
1717+
return
16721718
else
1673-
LAST_PARAM_IS_COLUMNS=0
1719+
COMPREPLY=( $(compgen -W "${COLUMNS[*]}" -S "," -- ${realcur}) )
1720+
compopt -o nospace
1721+
return
16741722
fi
1675-
break
16761723
fi
1677-
done
1724+
1725+
# user has finished entering column list or query
1726+
if [[ ${#COMP_WORDS[@]} == 6 ]];
1727+
then
1728+
COMPREPLY=( $(compgen -W "--output" -- $cur) )
1729+
return
1730+
fi
1731+
1732+
if [[ "${COMP_WORDS[5]}" == "--output"* ]];
1733+
then
1734+
if [[ "${COMP_WORDS[3]}" == "--columns"* ]];
1735+
then
1736+
COMPREPLY=( $(compgen -W "table text" -- $cur) )
1737+
return
1738+
else
1739+
COMPREPLY=( $(compgen -W "json json-pp text" -- $cur) )
1740+
return
1741+
fi
1742+
fi
1743+
1744+
return
1745+
fi
16781746
{% endraw %}
16791747
1748+
# if the last parameter was --*file perform
1749+
# file and directory autocompletion
1750+
if echo "${prev}" | grep -q '\-\-.*file$'
1751+
then
1752+
_filedir;
1753+
return
1754+
fi
1755+
1756+
# if last input was > for redirecting to a file
1757+
# perform file and directory autocompletion
1758+
if echo "${prev}" | grep -q '>'
1759+
then
1760+
_filedir;
1761+
return
1762+
fi
1763+
16801764
case "$COMP_WORDS_AS_STRING" in
16811765
16821766
{% set module_names = " ".join(modules.keys()) %}
@@ -1685,27 +1769,7 @@ def bash(self,):
16851769
{% for function_name in modules[module_name] %}
16861770
{% set param_names = " ".join(modules[module_name][function_name]).replace('_', '-') %}
16871771
{% if function_name == "list" %}
1688-
*"hpecp.{{module_name}}.{{function_name.replace('_', '-')}}."*)
1689-
PARAM_NAMES="{{param_names}}"
1690-
for PARAM in ${PARAM_NAMES[@]}; do
1691-
PARAM="${PARAM//'\'}"
1692-
for WORD in ${COMP_WORDS[@]}; do
1693-
if [[ "${WORD}" == "${PARAM}" ]]
1694-
then
1695-
# remove parameters already entered by user
1696-
PARAM_NAMES=${PARAM_NAMES//$WORD/}
1697-
fi
1698-
done
1699-
done
1700-
if [[ $LAST_PARAM_IS_COLUMNS == 1 ]]
1701-
then
1702-
{% set column_names = " ".join(columns[module_name]) %}
1703-
COMPREPLY=( $(compgen -W "{{column_names}}" -- $cur) )
1704-
COMPREPLY+=( $(compgen -W "$PARAM_NAMES" -- $cur) )
1705-
else
1706-
COMPREPLY=( $(compgen -W "$PARAM_NAMES" -- $cur) )
1707-
fi
1708-
;;
1772+
# do nothing - already handled above
17091773
{% else %}
17101774
*"hpecp.{{module_name}}.{{function_name.replace('_', '-')}}."*)
17111775
PARAM_NAMES="{{param_names}}"

0 commit comments

Comments
 (0)