├── .pre-commit-hooks.yaml ├── LICENSE.md ├── README.md └── bin ├── prom_config.sh ├── prom_rules.sh ├── tf_fmt.sh └── tf_validate.sh /.pre-commit-hooks.yaml: -------------------------------------------------------------------------------- 1 | - id: terraform_fmt 2 | name: terraform_fmt 3 | description: Formats terraform scripts into the correct checkstyle 4 | entry: bin/tf_fmt.sh 5 | language: script 6 | types: 7 | - terraform 8 | exclude: \.terraform\/.*$ 9 | 10 | - id: terraform_validate 11 | name: terraform_validate 12 | description: Validates terraform scripts syntax 13 | entry: bin/tf_validate.sh 14 | language: script 15 | types: 16 | - terraform 17 | exclude: \.terraform\/.*$ 18 | 19 | - id: prometheus_check_rules 20 | name: prometheus_check_rules 21 | description: Validates prometheus rules 22 | entry: bin/prom_rules.sh 23 | language: script 24 | files: \.rules$ 25 | 26 | - id: prometheus_check_config 27 | name: prometheus_check_config 28 | description: Validates prometheus configuration 29 | entry: bin/prom_config.sh 30 | language: script 31 | files: prometheus.yml$ 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Joel Bastos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Table of Contents 2 | ================= 3 | 4 | * [pre\-commit hooks](#pre-commit-hooks) 5 | * [Available Hooks](#available-hooks) 6 | * [How\-To](#how-to) 7 | * [Step 1](#step-1) 8 | * [Step 2](#step-2) 9 | * [Step 3](#step-3) 10 | * [Credits](#credits) 11 | 12 | # pre-commit hooks 13 | Collection of pre-commit hooks to be used with [Yelp's](http://pre-commit.com/) pre-commit framework 14 | 15 | ## Available Hooks 16 | * Terraform format 17 | * Terraform validate 18 | * Prometheus rules validation 19 | * Prometheus configuration validation 20 | 21 | Check the [hooks manifest](https://github.com/kintoandar/pre-commit/blob/master/.pre-commit-hooks.yaml) to know more 22 | 23 | ## How-To 24 | ### Step 1 25 | Install the pre-commit package 26 | 27 | ```shell 28 | brew install pre-commit 29 | ``` 30 | 31 | _For operating systems, other than macOS, check the [official documentation](http://pre-commit.com/#install)_ 32 | 33 | ### Step 2 34 | Step into the repo you want to have the pre-commit hooks installed and run: 35 | 36 | ```shell 37 | cat < .pre-commit-config.yaml 38 | - repo: git://github.com/kintoandar/pre-commit.git 39 | sha: v2.1.2 40 | hooks: 41 | - id: terraform_fmt 42 | - id: terraform_validate 43 | - id: prometheus_check_rules 44 | - id: prometheus_check_config 45 | EOF 46 | ``` 47 | 48 | ### Step 3 49 | Install the pre-commit hook 50 | 51 | ```shell 52 | pre-commit install 53 | ``` 54 | 55 | ## Credits 56 | Made with ♥️ by [kintoandar](https://blog.kintoandar.com) 57 | -------------------------------------------------------------------------------- /bin/prom_config.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | for file_with_path in "$@"; do 5 | promtool check config "$file_with_path" 6 | done 7 | -------------------------------------------------------------------------------- /bin/prom_rules.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | for file_with_path in "$@"; do 5 | promtool check rules "$file_with_path" 6 | done 7 | -------------------------------------------------------------------------------- /bin/tf_fmt.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | declare -a paths 5 | index=0 6 | 7 | for file_with_path in "$@"; do 8 | paths[index]=$(dirname "$file_with_path") 9 | let "index+=1" 10 | done 11 | 12 | for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do 13 | terraform fmt "$path_uniq" 14 | done 15 | -------------------------------------------------------------------------------- /bin/tf_validate.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | declare -a paths 5 | index=0 6 | 7 | for file_with_path in "$@"; do 8 | paths[index]=$(dirname "$file_with_path") 9 | let "index+=1" 10 | done 11 | 12 | for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do 13 | pushd "$path_uniq" > /dev/null 14 | terraform validate -check-variables=false 15 | popd > /dev/null 16 | done 17 | --------------------------------------------------------------------------------