From 2930506687bb04ef0facffb71dfc640adfea85e4 Mon Sep 17 00:00:00 2001 From: LanikSJ Date: Sun, 21 Jan 2018 11:07:48 -0800 Subject: [PATCH] Update Terraform Completion --- .../available/terraform.completion.bash | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/completion/available/terraform.completion.bash b/completion/available/terraform.completion.bash index 807a709a..11985f1d 100644 --- a/completion/available/terraform.completion.bash +++ b/completion/available/terraform.completion.bash @@ -1,26 +1,35 @@ #!/usr/bin/env bash # Bash Terraform completion +# Source: https://gist.github.com/cornfeedhobo/8bc08747ec3add1fc5adb2edb7cd68d3 -_terraform() -{ - local cmds cur colonprefixes - cmds="apply destroy fmt get graph import init \ - output plan push refresh remote show taint \ - untaint validate version state" +_terraform() { + local cur prev words cword opts + _get_comp_words_by_ref -n : cur prev words cword + COMPREPLY=() + opts="" - COMPREPLY=() - cur=${COMP_WORDS[COMP_CWORD]} - # Work-around bash_completion issue where bash interprets a colon - # as a separator. - # Work-around borrowed from the darcs work-around for the same - # issue. - colonprefixes=${cur%"${cur##*:}"} - COMPREPLY=( $(compgen -W '$cmds' -- $cur)) - local i=${#COMPREPLY[*]} - while [ $((--i)) -ge 0 ]; do - COMPREPLY[$i]=${COMPREPLY[$i]#"$colonprefixes"} - done + if [[ ${cur} == -* ]] ; then + compopt -o nospace + fi + + if [[ ${cword} -eq 1 ]] ; then + if [[ ${cur} == -* ]] ; then + opts="--help --version" + else + opts="$(terraform --help | grep -vE '(usage|Available|^$)' | awk '{print $1}')" + fi + fi + + if [[ ${cword} -gt 1 ]] ; then + if [[ ${cword} -eq 2 && ${prev} == '--help' ]] ; then + opts="$(terraform --help | grep -vE '(usage|Available|^$)' | awk '{print $1}')" + else + opts="$(terraform --help "${words[1]}" | grep '^ *-[a-z]' | awk '{print $1}' | awk -F '=' '{if ($0 ~ /=/) {print $1"="} else {print $1" "}}')" + fi + fi + + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 +} - return 0 -} && complete -F _terraform terraform