From 02a5acc390707d4e6a2c07b6fb095fbb1669e71e Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 28 Apr 2024 13:21:12 +0900 Subject: [PATCH] fix(completions/ssh): use "_omb_util_{split,expand_glob}" and add refactoring and performance improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: me --- completions/ssh.completion.sh | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/completions/ssh.completion.sh b/completions/ssh.completion.sh index 57fd125..94f6efa 100644 --- a/completions/ssh.completion.sh +++ b/completions/ssh.completion.sh @@ -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