From b89e8691736bcfc5ba165e61470388bd8fe39db3 Mon Sep 17 00:00:00 2001 From: yucuya Date: Tue, 25 May 2021 17:39:12 -0500 Subject: [PATCH 1/2] Adding bash completion support --- .bash_completion | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 .bash_completion diff --git a/.bash_completion b/.bash_completion new file mode 100644 index 0000000..5be24ea --- /dev/null +++ b/.bash_completion @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +# +# Ihsec bash completion +# ===================== +# +# Bash completion support for ihsec (https://github.com/daedreth/ihsec) +# +# Installation +# ------------ +# +# 1. For user completion, put this file under $HOME (see local completions in [5]): +# $HOME/.bash_completion +# +# 2. For system wide completion change the name of this file (i.e. ihsec.bash-completion) +# and put it in: +# /etc/bash-completion.d +# +# Usage +# ----- +# $ ihsec [tab] or [tab][tab] for options +# $ ihsec [tab][tab] for options or [tab] +# +# References +# ---------- +# Blogs and forums +# +# [1] https://blog.heckel.io/2015/03/24/bash-completion-with-sub-commands-and-dynamic-options/ +# [2] https://stackoverflow.com/questions/39624071/autocomplete-in-bash-script/39729507#39729507 +# +# Official documentation +# +# [3] https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion +# [4] https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion +# [5] https://github.com/scop/bash-completion +# +# Code; + +_ihsec() { + local cur prev dirs dirs_basenames + + cur=${COMP_WORDS[COMP_CWORD]} + prev=${COMP_WORDS[COMP_CWORD-1]} + + SUB_COMMANDS="list set del install" + # Subcommands completion. + COMPREPLY=( $(compgen -W "$SUB_COMMANDS" "$cur" ) ) + # Cases for subcommands which needs subdirs as arguments. + case "${prev}" in + del|set) + # Array of configs/subdirectories of ihsec. + dirs=( $(compgen -d "$HOME"/.ihsec/ ) ) + # Show only directories basenames. + dirs_basenames="${dirs[*]##*/}" + # Subdirectories completion. + COMPREPLY=( $(compgen -W "$dirs_basenames" "$cur") ) + + return 0 + ;; + *) # Does nothing in every other case. + ;; + esac +} +# Associate completion funtion with ihsec +complete -F _ihsec ihsec + +# ihsec bash completion ends here From 41b46fd53a2997da0f2149d8fa26faedd1e4ecda Mon Sep 17 00:00:00 2001 From: yucuya Date: Wed, 26 May 2021 10:37:41 -0500 Subject: [PATCH 2/2] Fixing command extension and change of redaction --- .bash_completion | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.bash_completion b/.bash_completion index 5be24ea..a10d633 100644 --- a/.bash_completion +++ b/.bash_completion @@ -3,7 +3,7 @@ # Ihsec bash completion # ===================== # -# Bash completion support for ihsec (https://github.com/daedreth/ihsec) +# Bash completion support with subcommands and subdirectories for ihsec (https://github.com/daedreth/ihsec) # # Installation # ------------ @@ -61,6 +61,6 @@ _ihsec() { esac } # Associate completion funtion with ihsec -complete -F _ihsec ihsec +complete -F _ihsec ihsec.sh # ihsec bash completion ends here