Skip to content

Instantly share code, notes, and snippets.

@devdrops
Created October 9, 2020 19:11
Show Gist options
  • Select an option

  • Save devdrops/5b367b52542787af66e9e48a5c389a53 to your computer and use it in GitHub Desktop.

Select an option

Save devdrops/5b367b52542787af66e9e48a5c389a53 to your computer and use it in GitHub Desktop.

Revisions

  1. devdrops created this gist Oct 9, 2020.
    20 changes: 20 additions & 0 deletions text.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # TF Diff - Checando diferenças nos arquivos terraform

    Trabalha com [Terraform](https://www.terraform.io/)?

    Costuma reforçar o uso de `terraform fmt` nos Pull Requests do seu time?

    Cansou de validar se os arquivos do Pull Request seguem corretamenta a sintaxe?

    ![](https://i.pinimg.com/originals/c9/30/2f/c9302f095cacd6df0ac144835071ff6d.gif)

    **SEUS PROBLEMAS ACABARAM!**

    Esse script em bash, ao ser executado, valida quais arquivos foram adicionados e/ou modificados, dizendo quais devem ser verificados.

    ### Requisitos

    - Docker
    - Bash
    - Git

    52 changes: 52 additions & 0 deletions tfdiff.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/bin/bash

    echo ""
    echo " ======================================================================== "
    echo " : ████████ ███████ ██████ ██ ███████ ███████ : "
    echo " : ██ ██ ██ ██ ██ ██ ██ : "
    echo " : ██ █████ ██ ██ ██ █████ █████ : "
    echo " : ██ ██ ██ ██ ██ ██ ██ : "
    echo " : ██ ██ ██████ ██ ██ ██ : "
    echo " ======================================================================== "
    echo ""

    echo " Check differences with terraform fmt on all changed .tf files."
    echo ""
    echo ""

    LIST=./list.txt
    if [[ -z "${BASE_BRANCH}" ]]; then
    export BASE_BRANCH="master"
    fi
    if [[ -z "${TF_VERSION}" ]]; then
    export TF_VERSION="0.11.14"
    fi

    echo " === Upgrading repository references ====================================>"
    echo ""

    git fetch --all > /dev/null 2>&1
    git diff origin/$BASE_BRANCH...$1 --name-only --diff-filter=AM | grep .tf > "$LIST"

    echo " === Target Branch: $1"
    echo " === Base Branch: $BASE_BRANCH"
    echo " === Terraform Version: $TF_VERSION"
    echo " === Files to check:"
    cat "$LIST"
    echo ""

    echo " === Checking files =====================================================>"
    while IFS= read -r file; do
    docker run --rm -v $(pwd):/code -w /code hashicorp/terraform:${TF_VERSION} fmt -check=true -list=false ./"$file"
    if [ $? = 0 ]; then
    printf " = OK: $file\n"
    else
    printf " = FAILED: $file\n"
    break
    fi
    done < "$LIST"
    rm "$LIST"
    echo ""

    echo " === Done! ==============================================================>"
    echo ""