Skip to content

Latest commit

 

History

History
432 lines (337 loc) · 17.8 KB

README.docker-terragrunt-fmt.md

File metadata and controls

432 lines (337 loc) · 17.8 KB

Docker image for terragrunt-fmt

Build Status Tag License

All #awesome-ci Docker images

ansible ansible-lint awesome-ci black checkmake eslint file-lint gofmt goimports golint jsonlint phpcbf phpcs phplint php-cs-fixer pycodestyle pylint terraform-docs terragrunt terragrunt-fmt yamlfmt yamllint

All #awesome-ci Makefiles

Visit cytopia/makefiles for seamless project integration, minimum required best-practice code linting and CI.

View Dockerfile on GitHub.

Docker hub

Tiny Alpine-based multistage-build dockerized version of Terraform[1] with the ability to do terraform fmt on Terragrunt files (.hcl). This is achieved by creating a temporary file within the container with an .tf extension and then running terraform fmt on it. Additionally the wrapper has been extended with a -ignore argument to be able to ignore files and directory or wildcards. The image is built nightly against multiple stable versions and pushed to Dockerhub.

[1] Official project: https://github.com/hashicorp/terraform

Available Docker image versions

The following Docker image tags are rolling releases and built and updated nightly. This means they always contain the latest stable version as shown below.

Docker tag Terraform version
latest latest stable
0.12 latest stable 0.12.x

Docker mounts

The working directory inside the Docker container is /data/ and should be mounted to your local filesystem where your Terragrant project resides. (See Examples for mount location usage.)

Usage

$ docker run --rm cytopia/terragrunt-fmt --help
Usage: cytopia/terragrunt-fmt [options] [DIR]
       cytopia/terragrunt-fmt --help
       cytopia/terragrunt-fmt --version

       Rewrites all Terragrunt configuration files to a canonical format. All
       hcl configuration files (.hcl) are updated.

       If DIR is not specified then the current working directory will be used.

Options:

  -list=true     List files whose formatting differs

  -write=false   Don't write to source files
                 (always disabled if using -check)

  -diff          Display diffs of formatting changes

  -check         Check if the input is formatted. Exit status will be 0 if all
                 input is properly formatted and non-zero otherwise.

  -recursive     Also process files in subdirectories. By default, only the
                 given directory (or current directory) is processed.

  -ignore=a,b    Comma separated list of paths to ignore.
                 The wildcard character '*' is supported.

Examples

List filenames that need to be fixed

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -list

[INFO] Finding files: for file in *.hcl; do
terraform fmt -list=true -write=true validate.hcl
../tmp/validate.hcl.tf

Show diff of files that need to be fixed

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -diff

[INFO] Finding files: for file in *.hcl; do
terraform fmt -list=true -write=false -diff validate.hcl
../tmp/validate.hcl.tf
--- old/../tmp/validate.hcl.tf
+++ new/../tmp/validate.hcl.tf
@@ -35,9 +35,9 @@
 # which is not being used (disable_init)
 remote_state {
   backend = "s3"
-  config   = {
-    bucket   = "none"
-    key     = "none"
+  config = {
+    bucket = "none"
+    key    = "none"
     region = "eu-central-1"
   }

Fix files

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -write

[INFO] Finding files: for file in *.hcl; do
terraform fmt -list=true -write=true validate.hcl
../tmp/validate.hcl.tf

Fix files and show diff

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -write -diff

[INFO] Finding files: for file in *.hcl; do
terraform fmt -list=true -write=false -diff validate.hcl
../tmp/validate.hcl.tf
--- old/../tmp/validate.hcl.tf
+++ new/../tmp/validate.hcl.tf
@@ -35,9 +35,9 @@
 # which is not being used (disable_init)
 remote_state {
   backend = "s3"
-  config   = {
-    bucket   = "none"
-    key     = "none"
+  config = {
+    bucket = "none"
+    key    = "none"
     region = "eu-central-1"
   }

List filenames that need to be fixed recursively

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -list -recursive

[INFO] Finding files: find . -name '*.hcl' -type f
terraform fmt -list=true -write=false ./prod/eu-central-1/microservice/terragrunt.hcl
../tmp/terragrunt.hcl.tf
terraform fmt -list=true -write=false ./prod/eu-central-1/infra/terragrunt.hcl
../tmp/terragrunt.hcl.tf

Show diff of files that need to be fixed recursively

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -diff -recursive

[INFO] Finding files: find . -name '*.hcl' -type f
terraform fmt -list=true -write=false -diff ./prod/eu-central-1/microservice/terragrunt.hcl
../tmp/terragrunt.hcl.tf
--- old/../tmp/terragrunt.hcl.tf
+++ new/../tmp/terragrunt.hcl.tf
@@ -1,5 +1,5 @@
 terraform {
-   source  = "github.com/cytopia/terraform-aws-iam-cross-account?ref=v0.1.3"
+  source  = "github.com/cytopia/terraform-aws-iam-cross-account?ref=v0.1.3"
 }
terraform fmt -list=true -write=false -diff ./prod/eu-central-1/infra/terragrunt.hcl
../tmp/terragrunt.hcl.tf
--- old/../tmp/terragrunt.hcl.tf
+++ new/../tmp/terragrunt.hcl.tf
@@ -1,5 +1,5 @@
 terraform {
-   source  = "github.com/cytopia/terraform-aws-iam-cross-account?ref=v0.1.3"
+  source  = "github.com/cytopia/terraform-aws-iam-cross-account?ref=v0.1.3"
 }

Fix recursively

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -write -recursive

[INFO] Finding files: find . -name '*.hcl' -type f
terraform fmt -list=true -write=true ./prod/eu-central-1/microservice/terragrunt.hcl
../tmp/terragrunt.hcl.tf
terraform fmt -list=true -write=true ./prod/eu-central-1/infra/terragrunt.hcl
../tmp/terragrunt.hcl.tf

Ignore files and directories

Ignore all files named terragrunt.hcl.

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -recursive -ignore=*terragrunt.hcl

[INFO] Finding files: find . -not \( -path "./*terragrunt.hcl*" \) -name '*.hcl' -type f
terraform fmt -list=true -write=false ./aws/validate.hcl
../tmp/validate.hcl.tf

Ignore all directories named dev/ and everything inside.

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -recursive -ignore=*/dev/

[INFO] Finding files: find . -not \( -path "./*/dev/*" \) -name '*.hcl' -type f
terraform fmt -list=true -write=false ./prod/eu-central-1/microservice/terragrunt.hcl
../tmp/terragrunt.hcl.tf
terraform fmt -list=true -write=false ./prod/eu-central-1/infra/terragrunt.hcl
../tmp/terragrunt.hcl.tf

Ignore all directories named dev/ and testing/ and everything inside.

$ docker run --rm -v $(pwd):/data cytopia/terragrunt-fmt -recursive -ignore=*/dev/,*/testing/

[INFO] Finding files: find . -not \( -path "./*/dev/*" -o -path "./*/testing/*" \) -name '*.hcl' -type f
terraform fmt -list=true -write=false ./prod/eu-central-1/microservice/terragrunt.hcl
../tmp/terragrunt.hcl.tf
terraform fmt -list=true -write=false ./prod/eu-central-1/infra/terragrunt.hcl
../tmp/terragrunt.hcl.tf

Project and CI integration

Makefile

You can add the following Makefile to your project for easy linting anf fixing of Terragrunt .hcl files.

ifneq (,)
.error This Makefile requires GNU Make.
endif

.PHONY: help lint fix _pull

CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

# Adjust according to your needs
IGNORE      = */.terragrunt-cache/,*/.terraform/
FMT_VERSION = latest

help:
	@echo "help    Show this help"
	@echo "lint    Exit > 0 if any files have wrong formatting"
	@echo "fix     Fix all .hcl files"

lint: _pull
	docker run --rm -v $(CURRENT_DIR):/data cytopia/terragrunt-fmt:$(FMT_VERSION) \
		-check -diff -recursive -ignore='$(IGNORE)'

fix: _pull
	docker run --rm -v $(CURRENT_DIR):/data cytopia/terragrunt-fmt:$(FMT_VERSION) \
		-write -diff -recursive -ignore='$(IGNORE)'

_pull:
	docker pull cytopia/terragrunt-fmt:$(FMT_VERSION)

Travis CI integration

With the above Makefile in place, you can easily add a Travis CI rule to ensure the Terragrunt code uses correct coding style.

---
sudo: required
language: minimal
services:
  - docker
script:
  - make lint

Related #awesome-ci projects

Docker images

Save yourself from installing lot's of dependencies and pick a dockerized version of your favourite linter below for reproducible local or remote CI tests:

GitHub DockerHub Type Description
awesome-ci aci-hub-img Basic Tools for git, file and static source code analysis
file-lint flint-hub-img Basic Baisc source code analysis
ansible ansible-hub-img Ansible Multiple versions and flavours of Ansible
ansible-lint alint-hub-img Ansible Lint Ansible
gofmt gfmt-hub-img Go Format Go source code [1]
goimports gimp-hub-img Go Format Go source code [1]
golint glint-hub-img Go Lint Go code
eslint elint-hub-img Javascript Lint Javascript code
jsonlint jlint-hub-img JSON Lint JSON files [1]
checkmake cm-hub-img Make Lint Makefiles
phpcbf pcbf-hub-img PHP PHP Code Beautifier and Fixer
phpcs pcs-hub-img PHP PHP Code Sniffer
phplint plint-hub-img PHP PHP Code Linter [1]
php-cs-fixer pcsf-hub-img PHP PHP Coding Standards Fixer
black black-hub-img Python The uncompromising Python code formatter
pycodestyle pycs-hub-img Python Python style guide checker
pylint pylint-hub-img Python Python source code, bug and quality checker
terraform-docs tfdocs-hub-img Terraform Terraform doc generator (TF 0.12 ready) [1]
terragrunt tg-hub-img Terraform Terragrunt and Terraform
terragrunt-fmt tgfmt-hub-img Terraform terraform fmt for Terragrunt files [1]
yamlfmt yfmt-hub-img Yaml Format Yaml files [1]
yamllint ylint-hub-img Yaml Lint Yaml files

[1] Uses a shell wrapper to add enhanced functionality not available by original project.

Makefiles

Visit cytopia/makefiles for dependency-less, seamless project integration and minimum required best-practice code linting for CI. The provided Makefiles will only require GNU Make and Docker itself removing the need to install anything else.

License

MIT License

Copyright (c) 2019 cytopia