Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ansible-vault. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 84 additions & 20 deletions ansible-vault-completion.bash
Original file line number Diff line number Diff line change
@@ -1,21 +1,85 @@
#!/bin/env bash

_ansible-vault() {
local current_word=${COMP_WORDS[COMP_CWORD]}
local previous_word=${COMP_WORDS[COMP_CWORD - 1]}
local options="create decrypt edit encrypt encrypt_string rekey view"

case $previous_word in
create|decrypt|edit|encrypt|encrypt_string|rekey|view)
options="--debug --vault-password-file -h --help"
if [[ "$current_word" == -* ]]; then
COMPREPLY=( $( compgen -W "$options" -- "$current_word" ) )
fi
;;
*)
COMPREPLY=( $( compgen -fdW "$options" -- "$current_word" ) )
;;
esac
}
_ansible_vault() {
local cur prev OPTS wanted
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

case $prev in
# a filename (or similar) MUST directly follow these flags.
# sugest nothing else
--vault-id|--encrypt-vault-id|--vault-password-file|--encrypt-vault-id|--new-vault-password-file|--new-vault-id)
# TODO not completely correct, since some of these allow
# special forms starting with `@' (and more?)
COMPREPLY=( $(compgen -A file -- $cur) )
return
;;
--output)
# `-' is stdout
COMPREPLY=( - $(compgen -A file -- $cur) )
return
;;
ansible-vault)
COMPREPLY+=( $(compgen -W '--version' -- $cur) )
;;
esac

COMPREPLY+=( $(compgen -W '-h --help -v -vvv -vvvv --verbose' -- $cur) )

# only sugest verbs when none is given
verbs='create decrypt edit view encrypt encrypt_string rekey'
none=
for verb in $verbs; do
if [[ " ${COMP_WORDS[@]} " =~ " $verb" ]]; then
contains=true
fi
done

if [ "x$contains" = "x" ]; then
COMPREPLY+=( $(compgen -W "$verbs" -- $cur) )
return
fi

# flags applicable to all verbs
wanted=( --vault-id
# TODO these two are multually exclusive
--ask-vault-password --vault-password-file
)

complete -o default -F _ansible-vault ansible-vault
# verb specific flags
case ${COMP_WORDS[1]} in
create|edit)
wanted+=( --encrypt-vault-id )
;;
decrypt)
wanted+=( --output )
;;
encrypt)
wanted+=( --output --encrypt-vault-id )
;;
encrypt_string)
wanted+=( --output --encrypt-vault-id -p --prompt -n --name --stdin-name )
;;
rekey)
wanted+=( --encrypt-vault-id
# TODO these two are multually exclusive
--new-vault-password-file --new-vault-id
)
;;
esac

# don't recommend the same flag twice
actually_wanted=()
for w in "${wanted[@]}"; do
if [[ ! " ${COMP_WORDS[@]} " =~ " $w" ]]; then
actually_wanted+=( $w )
fi
done

COMPREPLY+=( $(compgen -W "${actually_wanted[*]}" -- "${cur}" ) )

# everything except encrypt_string takes any number of files
if [ "${COMP_WORDS[1]}" != 'encrypt_string' ]; then
COMPREPLY+=( $(compgen -A file -- $cur) )
fi

}
complete -F _ansible_vault ansible-vault