Skip to content

Commit

Permalink
feat: tfupdate support
Browse files Browse the repository at this point in the history
  • Loading branch information
jrottenberg committed Feb 13, 2022
1 parent e88abb6 commit 5dbbaa0
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 27 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,12 @@
files: \.tf$
exclude: \.terraform\/.*$
require_serial: true

- id: tfupdate
name: tfupdate
description: Runs tfupdate on Terraform templates.
language: python
entry: hook_tfupdate
require_serial: true
always_run: true
pass_filenames: false
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ All notable changes to this project will be documented in this file.

- fix: Change terraform_validate hook functionality for subdirectories with terraform files ([#100](https://github.com/antonbabenko/pre-commit-terraform/issues/100))

###
###

configuration for the appropriate working directory.

Expand Down
68 changes: 46 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@ If you are using `pre-commit-terraform` already or want to support its developme

## Table of content

* [Sponsors](#sponsors)
* [Table of content](#table-of-content)
* [How to install](#how-to-install)
* [1. Install dependencies](#1-install-dependencies)
* [2. Install the pre-commit hook globally](#2-install-the-pre-commit-hook-globally)
* [3. Add configs and hooks](#3-add-configs-and-hooks)
* [4. Run](#4-run)
* [Available Hooks](#available-hooks)
* [Hooks usage notes and examples](#hooks-usage-notes-and-examples)
* [checkov](#checkov)
* [infracost_breakdown](#infracost_breakdown)
* [terraform_docs](#terraform_docs)
* [terraform_docs_replace (deprecated)](#terraform_docs_replace-deprecated)
* [terraform_fmt](#terraform_fmt)
* [terraform_providers_lock](#terraform_providers_lock)
* [terraform_tflint](#terraform_tflint)
* [terraform_tfsec](#terraform_tfsec)
* [terraform_validate](#terraform_validate)
* [terrascan](#terrascan)
* [Authors](#authors)
* [License](#license)
- [Collection of git hooks for Terraform to be used with pre-commit framework](#collection-of-git-hooks-for-terraform-to-be-used-with-pre-commit-framework)
- [Sponsors](#sponsors)
- [Table of content](#table-of-content)
- [How to install](#how-to-install)
- [1. Install dependencies](#1-install-dependencies)
- [2. Install the pre-commit hook globally](#2-install-the-pre-commit-hook-globally)
- [3. Add configs and hooks](#3-add-configs-and-hooks)
- [4. Run](#4-run)
- [Available Hooks](#available-hooks)
- [Hooks usage notes and examples](#hooks-usage-notes-and-examples)
- [checkov](#checkov)
- [infracost_breakdown](#infracost_breakdown)
- [terraform_docs](#terraform_docs)
- [terraform_docs_replace (deprecated)](#terraform_docs_replace-deprecated)
- [terraform_fmt](#terraform_fmt)
- [terraform_providers_lock](#terraform_providers_lock)
- [terraform_tflint](#terraform_tflint)
- [terraform_tfsec](#terraform_tfsec)
- [terraform_validate](#terraform_validate)
- [terrascan](#terrascan)
- [tfupdate](#tfupdate)
- [Authors](#authors)
- [License](#license)

## How to install

Expand Down Expand Up @@ -224,7 +226,8 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) | - |
| `terragrunt_fmt` | Reformat all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. | `terragrunt` |
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) | `terragrunt` |
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. [Hook notes](#terrascan) | `terrascan` |
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. [Hook notes](#terrascan) | `terrascan` |
| `tfupdate` | [tfupdate](https://github.com/minamijoyo/tfupdate) Update version constraints of Terraform core, providers, and modules. [Hook notes](#tfupdate) | `tfupdate` |
<!-- markdownlint-enable no-inline-html -->

Check the [source file](https://github.com/antonbabenko/pre-commit-terraform/blob/master/.pre-commit-hooks.yaml) to know arguments used for each hook.
Expand Down Expand Up @@ -617,6 +620,27 @@ Example:
3. Use `--skip-rules="ruleID1,ruleID2"` parameter to skip one or more rules globally while scanning (e.g.: `--args=--skip-rules="ruleID1,ruleID2"`).
4. Use the syntax `#ts:skip=RuleID optional_comment` inside a resource to skip the rule for that resource.

### tfupdate

Out of the box tfupdate will pin the terraform version

```yaml
- id: tfupdate
```

But you can pass `tfupdate` custom commands like `provider ${PROVIDER_NAME}` :

```yaml
- id: tfupdate
name: tfupdate terraform
- id: tfupdate
name: tfupdate provider vsphere
args:
- provider
- vsphere
```
See the `tfupdate --help` command line help for available options. No need to pass `--recursive .` , it is added automatically

## Authors

This repository is managed by [Anton Babenko](https://github.com/antonbabenko) with help from these awesome contributors:
Expand Down
4 changes: 0 additions & 4 deletions hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
print(
'`terraform_docs_replace` hook is DEPRECATED.'
'For details, see https://github.com/antonbabenko/pre-commit-terraform/issues/248'
)
4 changes: 4 additions & 0 deletions hooks/terraform_docs_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ def main(argv=None):


if __name__ == '__main__':
print(
'`terraform_docs_replace` hook is DEPRECATED.'
'For details, see https://github.com/antonbabenko/pre-commit-terraform/issues/248'
)
sys.exit(main())
28 changes: 28 additions & 0 deletions hooks/tfupdate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import subprocess
import sys


def main(argv=None):
args = sys.argv
del args[0]

retval = 0
procArgs = []
procArgs.append("tfupdate")
if len(args) == 0:
args = ['terraform']
procArgs += args
procArgs.append('-r')
procArgs.append('.')

try:

subprocess.check_call(procArgs)
except subprocess.CalledProcessError as e:
print(e)
retval = 1
return retval


if __name__ == '__main__':
sys.exit(main())
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
entry_points={
'console_scripts': [
'terraform_docs_replace = hooks.terraform_docs_replace:main',
'hook_tfupdate = hooks.tfupdate:main',
],
},
)

0 comments on commit 5dbbaa0

Please sign in to comment.