Created
December 22, 2021 21:34
-
-
Save chrispruitt/8a9dd606108809238a7ddeb7c54b3f50 to your computer and use it in GitHub Desktop.
This file contains 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
# enable terraform autocomplete for zsh | |
# complete -o nospace -C /usr/local/bin/terraform terraform | |
# alias execute terraform.sh wrapper in current directory | |
alias t="./terraform.sh" | |
_t() { | |
NAMESPACE_DIR=$(ls | grep namespaces) | |
WORKSPACE_DIR=$(ls | grep environments) | |
if [ ! ${WORKSPACE_DIR} ]; then | |
WORKSPACE_DIR=$(ls | grep workspaces) | |
fi | |
local cur prev | |
cur=${COMP_WORDS[COMP_CWORD]} | |
prev=${COMP_WORDS[COMP_CWORD-1]} | |
prev_prev=${COMP_WORDS[COMP_CWORD-2]} | |
case ${COMP_CWORD} in | |
1) | |
if [ ${NAMESPACE_DIR} ]; then | |
WORDS="$(ls $(pwd)/${NAMESPACE_DIR})" | |
else | |
WORDS="$(ls $(pwd)/${WORKSPACE_DIR})" | |
fi | |
COMPREPLY=($(compgen -W "$WORDS" -- "${cur}")) | |
;; | |
2) | |
if [ ${NAMESPACE_DIR} ]; then | |
WORDS="$(ls $(pwd)/${NAMESPACE_DIR}/${prev})" | |
else | |
WORDS="$(find $(pwd)/${WORKSPACE_DIR}/${prev} -type f -name '*.tf' -not -path '*.terraform/*' | sed -r 's|/[^/]+$||' | sort | uniq | sed 's|'`pwd`'/'${WORKSPACE_DIR}'/'${prev}'/||g')" | |
fi | |
COMPREPLY=($(compgen -W "$WORDS" -- "${cur}")) | |
;; | |
3) | |
if [ ${NAMESPACE_DIR} ]; then | |
WORDS="$(ls $(pwd)/${NAMESPACE_DIR}/${prev_prev}/${prev})" | |
WORDS="$(find $(pwd)/${NAMESPACE_DIR}/${prev_prev}/${prev} -type f -name '*.tf' -not -path '*.terraform/*' | sed -r 's|/[^/]+$||' | sort | uniq | sed 's|'`pwd`'/'${NAMESPACE_DIR}'/'${prev_prev}'/'${prev}'/||g')" | |
COMPREPLY=($(compgen -W "$WORDS" -- "${cur}")) | |
fi | |
;; | |
esac | |
} | |
complete -F _t t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment