Skip to content

Instantly share code, notes, and snippets.

@craigforr
Forked from zish/terraform_bash_completion.sh
Created November 5, 2020 21:31
Show Gist options
  • Save craigforr/af408a61cbc8558e4b078a0ea4670d5d to your computer and use it in GitHub Desktop.
Save craigforr/af408a61cbc8558e4b078a0ea4670d5d to your computer and use it in GitHub Desktop.

Revisions

  1. @zish zish revised this gist Jul 30, 2018. 1 changed file with 37 additions and 14 deletions.
    51 changes: 37 additions & 14 deletions terraform_bash_completion.sh
    Original file line number Diff line number Diff line change
    @@ -1,70 +1,87 @@
    # Bash Terraform completion
    # Adapted from cornfeedhobo's Gist at https://gist.github.com/cornfeedhobo/8bc08747ec3add1fc5adb2edb7cd68d3
    # Originally adapted from: https://gist.github.com/cornfeedhobo/8bc08747ec3add1fc5adb2edb7cd68d3
    #
    # Author: Jeremy Melanson
    #
    # Features of this update:
    # - Use built-in bash routines for text processing, instead of external tools (awk, sed, grep, ...).
    # - fixes the retrieval of options from the Terraform executble.
    # - Optional _init_terraform_completion function, which can enable command-completion for multiple Terraform executables.
    #
    # To use this tool, add the contents to your ${HOME}/.bashrc file.
    #

    #-- Get options listing from Terraform command.
    #--- Get options listing from Terraform command.
    _terraform_completion_get_opts () {
    local CMD_EXEC="${1}"
    local TF_OPT="${2}"

    local IFS=$'\n'
    #- "terraform --help"

    #-- "terraform -help"
    if [[ "${TF_OPT}" == "" ]]; then

    for O in $(${CMD_EXEC} -help); do
    if [[ "${O}" =~ ^\ +([^\ ]+) ]]; then
    echo "${BASH_REMATCH[1]}"

    fi
    done
    #- "terraform --help XXXX"

    #-- "terraform -help XXXX"
    else

    for O in $(${CMD_EXEC} -help ${TF_OPT}); do
    if [[ "${O}" =~ ^\ +(-[^\ =]+=?) ]]; then
    echo "${BASH_REMATCH[1]}"
    echo -e "${BASH_REMATCH[1]}"

    fi
    done
    fi
    }

    #-- This function is passed to 'complete' for handling completion.
    #--- This function is passed to 'complete' for handling completion.
    _terraform_completion () {
    local cur prev words cword opts
    _init_completion -s || return

    _get_comp_words_by_ref -n : cur prev words cword
    COMPREPLY=()

    opts=""

    if [[ ${cur} == -* ]] ; then
    compopt -o nospace

    fi

    local CMD_EXEC="${COMP_WORDS[0]}"

    if [[ ${cword} -eq 1 ]] ; then
    if [[ ${cur} == -* ]] ; then
    opts="--help --version"

    else
    opts="$(_terraform_completion_get_opts ${CMD_EXEC})"

    fi
    fi

    if [[ ${cword} -gt 1 ]] ; then
    elif [[ ${cword} -gt 1 ]]; then

    if [[ ${cword} -eq 2 && ${prev} =~ \-\-?help ]]; then
    opts="$(_terraform_completion_get_opts ${CMD_EXEC})"

    else
    opts="$(_terraform_completion_get_opts ${CMD_EXEC} ${prev})"
    local TF_COMMAND="${words[1]}"
    opts="$(_terraform_completion_get_opts ${CMD_EXEC} ${TF_COMMAND})"

    fi
    fi

    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )

    return 0
    }

    #-- Initialize Bash Command Completion for multiple Terraform executables.
    #--- Initialize Bash Command Completion for multiple Terraform executables.
    # It searches the PATH for files starting with "terraform", but does not
    # contain "-" characters. This avoids adding Completion for third-party
    # Terraform provider plugins that exist as a separate executable
    @@ -82,13 +99,16 @@ _init_terraform_completion () {
    for P in ${PATH}; do
    if [ -d "${P}" ]; then
    cd "${P}"

    else
    continue

    fi

    for E in *; do
    if [[ "${E}" =~ ${TF_EXEC_PREFIX} ]]; then
    complete -F _terraform_completion ${E}

    fi
    done
    done
    @@ -98,7 +118,7 @@ _init_terraform_completion () {

    complete -F _terraform_completion terraform

    #-- Optionally enable command completion for multiple Terraform executables.
    #--- Optionally enable command completion for multiple Terraform executables.
    # It currently works with executables in the PATH, that are named similar
    # to "terraform_XXXX".
    #
    @@ -108,4 +128,7 @@ complete -F _terraform_completion terraform
    # This provides a little simplicity, when working with multiple Terraform versions.
    #
    # Uncomment this line to enable:
    _init_terraform_completion
    _init_terraform_completion

    #--- Remove the initialization function. It is onluy needed once.
    unset -f _init_terraform_completion
  2. @zish zish created this gist Feb 6, 2018.
    111 changes: 111 additions & 0 deletions terraform_bash_completion.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    # Bash Terraform completion
    # Adapted from cornfeedhobo's Gist at https://gist.github.com/cornfeedhobo/8bc08747ec3add1fc5adb2edb7cd68d3
    #
    # Features of this update:
    # - Use built-in bash routines for text processing, instead of external tools (awk, sed, grep, ...).
    # - fixes the retrieval of options from the Terraform executble.
    # - Optional _init_terraform_completion function, which can enable command-completion for multiple Terraform executables.
    #
    # To use this tool, add the contents to your ${HOME}/.bashrc file.
    #

    #-- Get options listing from Terraform command.
    _terraform_completion_get_opts () {
    local CMD_EXEC="${1}"
    local TF_OPT="${2}"

    local IFS=$'\n'
    #- "terraform --help"
    if [[ "${TF_OPT}" == "" ]]; then
    for O in $(${CMD_EXEC} -help); do
    if [[ "${O}" =~ ^\ +([^\ ]+) ]]; then
    echo "${BASH_REMATCH[1]}"
    fi
    done
    #- "terraform --help XXXX"
    else
    for O in $(${CMD_EXEC} -help ${TF_OPT}); do
    if [[ "${O}" =~ ^\ +(-[^\ =]+=?) ]]; then
    echo "${BASH_REMATCH[1]}"
    fi
    done
    fi
    }

    #-- This function is passed to 'complete' for handling completion.
    _terraform_completion () {
    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

    local CMD_EXEC="${COMP_WORDS[0]}"

    if [[ ${cword} -eq 1 ]] ; then
    if [[ ${cur} == -* ]] ; then
    opts="--help --version"
    else
    opts="$(_terraform_completion_get_opts ${CMD_EXEC})"
    fi
    fi

    if [[ ${cword} -gt 1 ]] ; then
    if [[ ${cword} -eq 2 && ${prev} =~ \-\-?help ]]; then
    opts="$(_terraform_completion_get_opts ${CMD_EXEC})"
    else
    opts="$(_terraform_completion_get_opts ${CMD_EXEC} ${prev})"
    fi
    fi

    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
    }

    #-- Initialize Bash Command Completion for multiple Terraform executables.
    # It searches the PATH for files starting with "terraform", but does not
    # contain "-" characters. This avoids adding Completion for third-party
    # Terraform provider plugins that exist as a separate executable
    # (terraform-provider-XXXX).
    _init_terraform_completion () {
    local IFS=':'
    #-- Regex used when looking for terraform executables.
    # Looks for "terraform", or "terraform[anything else that isn't a dash]".
    # This enables Command Completion for multiple versions of Terraform,
    # if you have them.
    local TF_EXEC_PREFIX='^terrafor(m[^-]+|m)$'

    local ORIG_DIR="${PWD}"

    for P in ${PATH}; do
    if [ -d "${P}" ]; then
    cd "${P}"
    else
    continue
    fi

    for E in *; do
    if [[ "${E}" =~ ${TF_EXEC_PREFIX} ]]; then
    complete -F _terraform_completion ${E}
    fi
    done
    done

    cd "${ORIG_DIR}"
    }

    complete -F _terraform_completion terraform

    #-- Optionally enable command completion for multiple Terraform executables.
    # It currently works with executables in the PATH, that are named similar
    # to "terraform_XXXX".
    #
    # ** If your files are named differently, then you may need to modify the REGEX
    # ** in TF_EXEC_PREFIX to suit your needs.
    #
    # This provides a little simplicity, when working with multiple Terraform versions.
    #
    # Uncomment this line to enable:
    _init_terraform_completion