Skip to content

Instantly share code, notes, and snippets.

@chrispruitt
Created December 22, 2021 21:34
Show Gist options
  • Save chrispruitt/8a9dd606108809238a7ddeb7c54b3f50 to your computer and use it in GitHub Desktop.
Save chrispruitt/8a9dd606108809238a7ddeb7c54b3f50 to your computer and use it in GitHub Desktop.
# 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