-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile
143 lines (123 loc) · 5.91 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
ifneq (,)
.error This Makefile requires GNU Make.
endif
# -------------------------------------------------------------------------------------------------
# Default configuration
# -------------------------------------------------------------------------------------------------
.PHONY: help lint lint-files terraform-docs terraform-fmt _pull-tf _pull-tfdocs
CURRENT_DIR = $(PWD)
# -------------------------------------------------------------------------------------------------
# Docker image versions
# -------------------------------------------------------------------------------------------------
TF_VERSION = 1.3.9
FL_VERSION = latest-0.8
FL_IGNORE_PATHS = .git/,.github/,.idea/
# -------------------------------------------------------------------------------------------------
# Terraform-docs configuration
# -------------------------------------------------------------------------------------------------
TFDOCS_VERSION = 0.16.0-0.34
# Adjust your delimiter here or overwrite via make arguments
TFDOCS_DELIM_START = <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
TFDOCS_DELIM_CLOSE = <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
# -------------------------------------------------------------------------------------------------
# Meta Targets
# -------------------------------------------------------------------------------------------------
help:
@echo
@echo "Meta targets"
@echo "--------------------------------------------------------------------------------"
@echo " help Show this help screen"
@echo
@echo "Read-only targets"
@echo "--------------------------------------------------------------------------------"
@echo " lint Lint basics as well as *.tf and *.tfvars files"
@echo " lint-files Lint basics"
@echo
@echo "Writing targets"
@echo "--------------------------------------------------------------------------------"
@echo " terraform-docs Run terraform-docs against all README.md"
@echo " terraform-fmt Run terraform-fmt against *.tf and *.tfvars files"
# -------------------------------------------------------------------------------------------------
# Read-only Targets
# -------------------------------------------------------------------------------------------------
lint:
@$(MAKE) --no-print-directory terraform-fmt _WRITE=false
@$(MAKE) --no-print-directory lint-files
lint-files:
@echo "################################################################################"
@echo "# file-lint"
@echo "################################################################################"
@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '$(FL_IGNORE_PATHS)' --path .
@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '$(FL_IGNORE_PATHS)' --path .
@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '$(FL_IGNORE_PATHS)' --path .
@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '$(FL_IGNORE_PATHS)' --path .
@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '$(FL_IGNORE_PATHS)' --path .
@docker run --rm -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '$(FL_IGNORE_PATHS)' --path .
# -------------------------------------------------------------------------------------------------
# Writing Targets
# -------------------------------------------------------------------------------------------------
terraform-docs: _pull-tfdocs
@echo "################################################################################"
@echo "# Terraform-docs generate"
@echo "################################################################################"
@echo
@if docker run --rm $$(tty -s && echo "-it" || echo) \
-v "$(CURRENT_DIR):/data" \
-e TFDOCS_DELIM_START='$(TFDOCS_DELIM_START)' \
-e TFDOCS_DELIM_CLOSE='$(TFDOCS_DELIM_CLOSE)' \
cytopia/terraform-docs:$(TFDOCS_VERSION) \
terraform-docs-replace --sort-by required markdown README.md; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi;
@echo
terraform-fmt: _WRITE=true
terraform-fmt: _pull-tf
@# Lint all Terraform files
@echo "################################################################################"
@echo "# Terraform fmt"
@echo "################################################################################"
@echo
@echo "------------------------------------------------------------"
@echo "# *.tf files"
@echo "------------------------------------------------------------"
@if docker run $$(tty -s && echo "-it" || echo) --rm \
-v "$(PWD):/data" hashicorp/terraform:$(TF_VERSION) fmt \
$$(test "$(_WRITE)" = "false" && echo "-check" || echo "-write=true") \
-diff \
-list=true \
-recursive \
/data; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi;
@echo
@echo "------------------------------------------------------------"
@echo "# *.tfvars files"
@echo "------------------------------------------------------------"
@if docker run $$(tty -s && echo "-it" || echo) --rm --entrypoint=/bin/sh \
-v "$(PWD):/data" hashicorp/terraform:$(TF_VERSION) \
-c "find . -not \( -path './*/.terragrunt-cache/*' -o -path './*/.terraform/*' \) \
-name '*.tfvars' -type f -print0 \
| xargs -0 -n1 terraform fmt \
$$(test '$(_WRITE)' = 'false' && echo '-check' || echo '-write=true') \
-diff \
-list=true"; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi;
@echo
# -------------------------------------------------------------------------------------------------
# Helper Targets
# -------------------------------------------------------------------------------------------------
# Ensure to always have the latest Terraform version
_pull-tf:
docker pull hashicorp/terraform:$(TF_VERSION)
_pull-tfdocs:
docker pull cytopia/terraform-docs:$(TFDOCS_VERSION)