From 1a357f5f8929bd5454a85c1ec80c5eb67c9f64d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ed=C3=AAnis=20Freindorfer=20Azevedo?= Date: Tue, 31 Aug 2021 10:21:38 -0300 Subject: [PATCH] Refactor `__lxc_groups()` to `__lxc_get_groups()`. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Make code logic be more clear to what it is being done. Signed-off-by: Edênis Freindorfer Azevedo --- config/bash/lxc.in | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/config/bash/lxc.in b/config/bash/lxc.in index 5380fc964..fa82c39fd 100644 --- a/config/bash/lxc.in +++ b/config/bash/lxc.in @@ -88,7 +88,7 @@ __lxc_get_snapshots() { if [[ ! "${snaps[*]}" =~ ${nosnapxp} ]]; then for i in "${!snaps[@]}"; do read -r -e -a line <<< "${snaps[${i}]}" - printf "%s " "${line[0]}" + command printf "%s " "${line[0]}" done fi } @@ -132,7 +132,7 @@ __lxc_concat_array_sep() { concat+="${word}${sep}" fi done - printf "%s" "${concat}" + command printf "%s" "${concat}" } __lxc_check_completion_avail() { @@ -308,14 +308,22 @@ _lxc_attach() { } && complete -F _lxc_attach lxc-attach -__lxc_groups() { +__lxc_get_groups() { declare -A groups - for line in "$(command lxc-ls -f --fancy-format GROUPS | command sed -e '/^-/d' -e '1d' -e 's/,/ /g')"; do - for grp in $line; do - groups+=([${grp}]=1) + local key + declare -a linegroups + # Discard "GROUPS" header and lines without any groups (with only '-'). + mapfile -s 1 -t lines < <(command lxc-ls -f --fancy-format GROUPS | command sed -e '/^-/d') + for line in "${lines[@]}"; do + line=$(command echo -e "${line}" | command sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + IFS=$',' read -r -e -a linegroups <<< "${line}" + for entry in "${linegroups[@]}"; do + key=$(printf "%q" "${entry}") + groups+=(["${key}"]=1) done done - printf "%s " "${!groups[@]}" + declare -a output=("${!groups[@]}") + command printf "%s" "${output[*]}" } _lxc_autostart() { @@ -335,7 +343,7 @@ _lxc_autostart() { return ;; --groups | -g ) - __lxc_piped_args "${cur}" ',' $( __lxc_groups ) ',' + __lxc_piped_args "${cur}" ',' $( __lxc_get_groups ) ',' return ;; esac @@ -758,7 +766,7 @@ _lxc_ls() { return ;; --groups | -g ) - __lxc_piped_args "${cur}" ',' $( __lxc_groups ) ',' + __lxc_piped_args "${cur}" ',' $( __lxc_get_groups ) ',' return ;; --nesting ) -- 2.47.2