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

Adding latest vault completion #860

Merged
merged 1 commit into from
Dec 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 29 additions & 14 deletions completion/available/vault.completion.bash
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
# Credit https://github.com/iljaweis/vault-bash-completion/
# ---------------------------------------------------------------------------
# vault-bash-completion
#
# This adds bash completions for [HashiCorp Vault](https://www.vaultproject.io/)
#
# see https://github.com/iljaweis/vault-bash-completion
# ---------------------------------------------------------------------------

function _vault() {
function _vault_mounts() {
(
set -euo pipefail
if ! vault mounts 2> /dev/null | awk 'NR > 1 {print $1}'; then
echo "secret"
fi
)
}

function _vault() {
local VAULT_COMMANDS='delete path-help read renew revoke server status write audit-disable audit-enable audit-list auth auth-disable auth-enable capabilities generate-root init key-status list mount mount-tune mounts policies policy-delete policy-write rekey remount rotate seal ssh step-down token-create token-lookup token-renew token-revoke unmount unseal version'

# get root paths
vault mounts >/dev/null 2>&1
if [ $? != 0 ]; then
# we do not have access to list mounts
local VAULT_ROOTPATH="secret"
else
local VAULT_ROOTPATH=$(vault mounts | tail -n +2 | awk '{print $1}' | paste -s -d ' ' -)
local cur
local prev

if [ $COMP_CWORD -gt 0 ]; then
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
fi

local cur=${COMP_WORDS[COMP_CWORD]}
local line=${COMP_LINE}

if [ "$(echo $line | wc -w)" -le 2 ]; then
if [[ $prev =~ ^(policies|policy-write|policy-delete) ]]; then
local policies=$(vault policies 2> /dev/null)
COMPREPLY=($(compgen -W "$policies" -- $cur))
elif [ "$(echo $line | wc -w)" -le 2 ]; then
if [[ "$line" =~ ^vault\ (read|write|delete|list)\ $ ]]; then
COMPREPLY=($(compgen -W "$VAULT_ROOTPATH" -- ''))
COMPREPLY=($(compgen -W "$(_vault_mounts)" -- ''))
else
COMPREPLY=($(compgen -W "$VAULT_COMMANDS" -- $cur))
fi
elif [[ "$line" =~ ^vault\ (read|write|delete|list)\ (.*)$ ]]; then
path=${BASH_REMATCH[2]}
if [[ "$path" =~ ^([^ ]+)/([^ /]*)$ ]]; then
list=$(vault list ${BASH_REMATCH[1]} | tail -n +2)
list=$(vault list -format=yaml ${BASH_REMATCH[1]} 2> /dev/null | awk '{ print $2 }')
COMPREPLY=($(compgen -W "$list" -P "${BASH_REMATCH[1]}/" -- ${BASH_REMATCH[2]}))
else
COMPREPLY=($(compgen -W "$VAULT_ROOTPATH" -- $path))
COMPREPLY=($(compgen -W "$(_vault_mounts)" -- $path))
fi
fi
}
Expand Down