From 3d31e7113ee403622b95af19c1fafbcbe1840445 Mon Sep 17 00:00:00 2001 From: Filippo Bonazzi Date: Tue, 21 Jun 2022 15:24:44 +0200 Subject: [PATCH] Add completion script --- secbox-completion.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 secbox-completion.sh diff --git a/secbox-completion.sh b/secbox-completion.sh new file mode 100644 index 0000000..ca687bf --- /dev/null +++ b/secbox-completion.sh @@ -0,0 +1,61 @@ +# Bash completion for secbox +# 2022 Filippo Bonazzi (filippo dot bonazzi at suse dot com) + +_generate_secbox_cmds_file() +{ + # Check that we have the output folder parameter + if [ "$#" -ne 1 ] + then + >&2 echo "usage: ${FUNCNAME[0]} " + return 1 + fi + + # Check if output directory exists and is writeable + if [[ -d "$1" ]] && [[ -x "$1" ]] && [[ -w "$1" ]] + then + secbox bash -c "compgen -c" > "$1/secbox_cmds.lst" + else + >&2 echo "Bad folder: $1" + >&2 echo "usage: ${FUNCNAME[0]} " + return 1 + fi +} + +_secbox_completion() +{ + local cur prev opts + + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + # Get list of commands available in secbox from file + # TODO: how do we find the file? + cmds=$(tr '\n' ' ' < secbox_cmds.lst) + + opts="--sshfs --nfs --destroy --root --debug --update-container --no-color --alias --tty --no-tty --interactive --no-interactive -h --help -v --version" + + # If we are completing the first time, complete on commands and options + if [[ $COMP_CWORD -eq 1 ]] + then + COMPREPLY=( $( compgen -W "$cmds $opts" -- "$cur" ) ) + return 0 + fi + + # Otherwise complete with more context + case "$prev" in + --destroy) + COMPREPLY=( $( compgen -W '-i -f' -- "$cur" ) ) + ;; + --update-container) + COMPREPLY=( $( compgen -W '-f --force' -- "$cur" ) ) + ;; + -*) + COMPREPLY=( $( compgen -W "$opts" -- "$cur" ) ) + ;; + esac + + } + +# Use _secbox_completion to generate completion for secbox +complete -F _secbox_completion secbox