fix(completions/ssh): use "_omb_util_{split,expand_glob}" and add refactoring and performance improvements

* refactor(completions/ssh): move declaration of local variables
* perf(completions/ssh): include the for-loop inside the if-statement of ~/.ssh/config
* refactor(completions/ssh): use glob matching
* perf(completions/ssh): read config files in a single grep&awk
* perf(completions/ssh): match ^Host by awk to reduce use of grep
* fix(completions/ssh): use "_omb_util_{split,expand_glob}" for safer expansions
* refactor(completions/ssh): rename local variables
* refactor(completions/ssh): perform pathname expansion at once
* fix(completions/ssh): correct to array var in relative or absolute transform loop

Co-authored-by: Stéphane Juventy <sjuventy.ext@orange.com>
Co-authored-by: me <stephane.juventy@gmail.com>
This commit is contained in:
Koichi Murase
2024-04-28 13:21:12 +09:00
parent d9711d8637
commit 02a5acc390

View File

@ -5,8 +5,6 @@ _omb_module_require lib:omb-completion
function _omb_completion_ssh {
local cur
local additional_include_option
local additional_include_defined_file
_omb_completion_reassemble_breaks :
if [[ $cur == *@* ]] ; then
@ -15,21 +13,32 @@ function _omb_completion_ssh {
local -a options=(-- "$cur")
fi
local IFS=$'\n'
# parse all defined hosts from .ssh/config
if [[ -r $HOME/.ssh/config ]]; then
COMPREPLY=($(compgen -W "$(grep ^Host "$HOME/.ssh/config" | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
fi
local -a config_files=("$HOME/.ssh/config")
# check if .ssh/config contains Include options
for additional_include_option in $(awk -F' ' '/^Include/{print $2}' "$HOME/.ssh/config" 2>/dev/null) ;do
# relative or absolute path, if relative transforms to absolute
[[ "${additional_include_option:0:1}" == "/" ]] ||additional_include_option="$HOME/.ssh/$additional_include_option"
# for loop to interpret possible globbing
for additional_include_defined_file in $additional_include_option ;do
# parse all defined hosts from that file
[[ -s "$additional_include_defined_file" ]] &&COMPREPLY+=($(compgen -W "$(grep ^Host "$additional_include_defined_file" | awk '{for (i=2; i<=NF; i++) print $i}' )" "${options[@]}"))
# check if .ssh/config contains Include options
local -a include_patterns
_omb_util_split include_patterns "$(awk -F' ' '/^Include/{print $2}' "$HOME/.ssh/config" 2>/dev/null)" $'\n'
local i
for i in "${!include_patterns[@]}"; do
# relative or absolute path, if relative transforms to absolute
[[ ${include_patterns[i]} == /* ]] || include_patterns[i]=~/.ssh/${include_patterns[i]}
done
done
# interpret possible globbing
local -a include_files
_omb_util_glob_expand include_files '${include_patterns[*]}'
local include_file
for include_file in "${include_files[@]}";do
# parse all defined hosts from that file
[[ -s "$include_file" ]] && config_files+=("$include_file")
done
COMPREPLY+=($(compgen -W "$(awk '/^Host/ {for (i=2; i<=NF; i++) print $i}' "${config_files[@]}")" "${options[@]}"))
fi
# parse all hosts found in .ssh/known_hosts
if [[ -r $HOME/.ssh/known_hosts ]]; then