Last active
March 4, 2017 23:32
-
-
Save Erliz/88fb01e6d36bceb2ae37d27b7d8dc219 to your computer and use it in GitHub Desktop.
Add bash autocomplete for hashicorp vault application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Path function, write override exist values in path | |
_vault-patch() { | |
path="" | |
if tty -s; then | |
if [ $# -le 2 ]; then | |
echo "No arguments specified. Usage:"; | |
echo "vault-patch secret/path field value"; | |
return 1; | |
fi | |
text="\"${@:3}\"" | |
echo {$2:$text} | |
vault read --format=json $1 | jq .data | jq ".+{$2:$text}" | vault write $1 - | |
fi; | |
} | |
# alias for read with json preview | |
vr() { | |
if [ $# -eq 2 ]; then | |
query=".data.$2" | |
else | |
query=".data" | |
fi | |
data=$(vault read --format=json $1 | jq -jr $query) | |
printf $data | pbcopy | |
echo $data | |
} | |
alias vw="vault write" | |
alias vp="_vault-patch" | |
function _vault_get_path() { | |
cur=$1 | |
if [[ ${cur} != *"/"* ]] ; then | |
words=$(vault mounts | tail -n $((`vault mounts|wc -l`-1)) | awk '{print $1}' | tr '\n' ' ') | |
suggestions=$(compgen -W "${words}" -- ${cur}) | |
else | |
if [[ ${cur} =~ /$ ]] ; then | |
space=${cur} | |
else | |
space="${cur%/*}/" | |
fi | |
list=$(vault list ${space}) | |
words=$(echo $list | cut -d " " -f3-) | |
suggestions=$(compgen -P ${space} -W "${words}" -- ${cur##*/}) | |
fi | |
echo $suggestions | |
} | |
function _vault() { | |
COMPREPLY=() | |
cur="${COMP_WORDS[COMP_CWORD]}" | |
if [[ ${COMP_CWORD} == 1 ]] ; then | |
COMPREPLY=( $(compgen -W "$(vault 2>&1 | egrep '^ +' | awk '{print $1}')" -- ${cur}) ) | |
return 0 | |
fi | |
compopt -o nospace | |
COMPREPLY=( $(_vault_get_path $cur) ) | |
} | |
function _vault_for_alias() { | |
COMPREPLY=() | |
cur="${COMP_WORDS[COMP_CWORD]}" | |
compopt -o nospace | |
COMPREPLY=( $(_vault_get_path $cur) ) | |
} | |
complete -o default -F _vault vault | |
complete -o default -F _vault_for_alias vr vw vp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment