From b538712dc302cefe472ba287fcb31dffd61623d3 Mon Sep 17 00:00:00 2001 From: Vangelis Tasoulas Date: Thu, 19 Apr 2018 18:34:00 +0200 Subject: [PATCH 1/5] Rewrote the terraform bash completion Signed-off-by: Vangelis Tasoulas --- .../available/terraform.completion.bash | 64 +++++++++++-------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/completion/available/terraform.completion.bash b/completion/available/terraform.completion.bash index 11985f1d..6052a33e 100644 --- a/completion/available/terraform.completion.bash +++ b/completion/available/terraform.completion.bash @@ -1,35 +1,49 @@ #!/usr/bin/env bash -# Bash Terraform completion -# Source: https://gist.github.com/cornfeedhobo/8bc08747ec3add1fc5adb2edb7cd68d3 +# Bash completion for the terraform command +# +# Copyright (c) 2018 Vangelis Tasoulas . _terraform() { - local cur prev words cword opts - _get_comp_words_by_ref -n : cur prev words cword - COMPREPLY=() - opts="" + local cur prev words cword opts + _get_comp_words_by_ref -n : cur prev words cword + COMPREPLY=() + opts="" - if [[ ${cur} == -* ]] ; then - compopt -o nospace - fi + if [[ ${cword} -eq 1 ]] ; then - if [[ ${cword} -eq 1 ]] ; then - if [[ ${cur} == -* ]] ; then - opts="--help --version" - else - opts="$(terraform --help | grep -vE '(usage|Available|^$)' | awk '{print $1}')" - fi - fi + # Options that do not start with a hyphen, are always starting with four spaces. + opts="$(terraform --help | grep -P '^\s\s\s\s\S' | awk '{print $1}')" + opts="${opts} --help --version" - 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 + elif [[ ${cword} -gt 1 ]] ; then - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 + if [[ ${cword} -eq 2 && ${prev} == '--help' ]] ; then + + opts="$(terraform --help | grep -P '^\s\s\s\s\S' | awk '{print $1}')" + + elif [[ ${words[1]} != "--help" && ${words[1]} != "--version" && ${words[1]} != "version" ]] ; then + + # Some commands acceps hyphened parameters,... + opts="$(terraform --help "${words[1]}" | grep -P '^\s+-' | awk '{print $1}' | awk -F '=' '{ if ($0 ~ /=/) {print $1"="} else {print $1} }')" + # but some other commands accept non hyphened parameters.... + opts="${opts} $(terraform --help "${words[1]}" | grep -P '^\s\s\s\s\S' | awk '{print $1}')" + # All of the commands accept the --help parameter which is not listed + # by the 'terraform --help + opts="${opts} --help" + + fi + fi + + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + + if [[ ${#COMPREPLY[*]} -eq 1 ]] ; then + if [[ ${COMPREPLY[0]} == *= ]]; then + # When only one completion is left, check if there is an equal sign. + # If an equal sign, then add no space after the autocompleted word. + compopt -o nospace; + fi + fi + return 0 } complete -F _terraform terraform From 880ea9f07ea9957df81af07528798f18e8c5b1f0 Mon Sep 17 00:00:00 2001 From: Vangelis Tasoulas Date: Thu, 19 Apr 2018 18:40:22 +0200 Subject: [PATCH 2/5] Added GPL2 header to the terraform bash completion script. Signed-off-by: Vangelis Tasoulas --- completion/available/terraform.completion.bash | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/completion/available/terraform.completion.bash b/completion/available/terraform.completion.bash index 6052a33e..12bf218e 100644 --- a/completion/available/terraform.completion.bash +++ b/completion/available/terraform.completion.bash @@ -1,7 +1,22 @@ #!/usr/bin/env bash +# # Bash completion for the terraform command +# +# Copyright (C) 2018 Vangelis Tasoulas # -# Copyright (c) 2018 Vangelis Tasoulas . +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. _terraform() { local cur prev words cword opts From 9efc944f19092a9616c2a7cddb739f4394e442ad Mon Sep 17 00:00:00 2001 From: Vangelis Tasoulas Date: Thu, 19 Apr 2018 18:42:13 +0200 Subject: [PATCH 3/5] The capistrano completion script should not have the executable bit set No other script in the completion/available folder does. Signed-off-by: Vangelis Tasoulas --- completion/available/capistrano.completion.bash | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 completion/available/capistrano.completion.bash diff --git a/completion/available/capistrano.completion.bash b/completion/available/capistrano.completion.bash old mode 100755 new mode 100644 From 039a677bd89963cd08a778807a5093a27bb3b34a Mon Sep 17 00:00:00 2001 From: Vangelis Tasoulas Date: Thu, 19 Apr 2018 18:50:00 +0200 Subject: [PATCH 4/5] Corrected a typo Signed-off-by: Vangelis Tasoulas --- completion/available/terraform.completion.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/completion/available/terraform.completion.bash b/completion/available/terraform.completion.bash index 12bf218e..1836c5ba 100644 --- a/completion/available/terraform.completion.bash +++ b/completion/available/terraform.completion.bash @@ -38,9 +38,9 @@ _terraform() { elif [[ ${words[1]} != "--help" && ${words[1]} != "--version" && ${words[1]} != "version" ]] ; then - # Some commands acceps hyphened parameters,... + # Some commands accept hyphened parameters, ... opts="$(terraform --help "${words[1]}" | grep -P '^\s+-' | awk '{print $1}' | awk -F '=' '{ if ($0 ~ /=/) {print $1"="} else {print $1} }')" - # but some other commands accept non hyphened parameters.... + # but some other commands accept non-hyphened parameters. opts="${opts} $(terraform --help "${words[1]}" | grep -P '^\s\s\s\s\S' | awk '{print $1}')" # All of the commands accept the --help parameter which is not listed # by the 'terraform --help From fd01635e4abe891998bcc3f81a73880ef9ca8237 Mon Sep 17 00:00:00 2001 From: Vangelis Tasoulas Date: Thu, 19 Apr 2018 18:54:29 +0200 Subject: [PATCH 5/5] Code consistency improvements Signed-off-by: Vangelis Tasoulas --- completion/available/terraform.completion.bash | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/completion/available/terraform.completion.bash b/completion/available/terraform.completion.bash index 1836c5ba..9fe0795a 100644 --- a/completion/available/terraform.completion.bash +++ b/completion/available/terraform.completion.bash @@ -18,7 +18,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -_terraform() { +_terraform() +{ local cur prev words cword opts _get_comp_words_by_ref -n : cur prev words cword COMPREPLY=() @@ -52,10 +53,10 @@ _terraform() { COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) if [[ ${#COMPREPLY[*]} -eq 1 ]] ; then - if [[ ${COMPREPLY[0]} == *= ]]; then + if [[ ${COMPREPLY[0]} == *= ]] ; then # When only one completion is left, check if there is an equal sign. # If an equal sign, then add no space after the autocompleted word. - compopt -o nospace; + compopt -o nospace fi fi return 0