@@ -2,95 +2,240 @@ ifneq (,)
22.error This Makefile requires GNU Make.
33endif
44
5+ .PHONY : help gen lint test _gen-main _gen-examples _gen-modules _lint-files _lint-fmt _lint-json _pull-tf _pull-tfdocs _pull-fl _pull-jl
6+
7+ CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
8+ TF_EXAMPLES = $(sort $(dir $(wildcard $(CURRENT_DIR ) examples/* /) ) )
9+ TF_MODULES = $(sort $(dir $(wildcard $(CURRENT_DIR ) modules/* /) ) )
10+ FL_IGNORE_PATHS = .git/,.github/,.terraform/,.idea/
11+
512# -------------------------------------------------------------------------------------------------
6- # Default configuration
13+ # Container versions
714# -------------------------------------------------------------------------------------------------
8- .PHONY : help lint lint-files terraform-docs terraform-fmt _pull-tf _pull-tfdocs
9- CURRENT_DIR = $(PWD )
10-
15+ TF_VERSION = 1.5.7
16+ TFDOCS_VERSION = 0.16.0-0.34
17+ FL_VERSION = latest-0.8
18+ JL_VERSION = 1.6.0-0.14
1119
1220# -------------------------------------------------------------------------------------------------
13- # Docker image versions
21+ # Enable linter (file-lint, terraform fmt, jsonlint)
1422# -------------------------------------------------------------------------------------------------
15- TF_VERSION = 1.3.9
16- FL_VERSION = latest-0.8
23+ LINT_FL_ENABLE = 1
24+ LINT_TF_ENABLE = 1
25+ LINT_JL_ENABLE = 1
1726
18- FL_IGNORE_PATHS = .git/,.github/,.idea/
1927
2028# -------------------------------------------------------------------------------------------------
21- # Terraform -docs configuration
29+ # terraform -docs defines
2230# -------------------------------------------------------------------------------------------------
23- TFDOCS_VERSION = 0.16.0-0.34
24-
2531# Adjust your delimiter here or overwrite via make arguments
26- TFDOCS_DELIM_START = <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
27- TFDOCS_DELIM_CLOSE = <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
32+ DELIM_START = <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
33+ DELIM_CLOSE = <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
34+ # What arguments to append to terraform-docs command
35+ TFDOCS_ARGS = --sort=false
36+
2837
2938# -------------------------------------------------------------------------------------------------
30- # Meta Targets
39+ # Default target
3140# -------------------------------------------------------------------------------------------------
32-
3341help :
34- @echo
35- @echo " Meta targets"
36- @echo " --------------------------------------------------------------------------------"
37- @echo " help Show this help screen"
38- @echo
39- @echo " Read-only targets"
40- @echo " --------------------------------------------------------------------------------"
41- @echo " lint Lint basics as well as *.tf and *.tfvars files"
42- @echo " lint-files Lint basics"
43- @echo
44- @echo " Writing targets"
45- @echo " --------------------------------------------------------------------------------"
46- @echo " terraform-docs Run terraform-docs against all README.md"
47- @echo " terraform-fmt Run terraform-fmt against *.tf and *.tfvars files"
42+ @echo " gen Generate terraform-docs output and replace in README.md's"
43+ @echo " lint Static source code analysis"
44+ @echo " test Integration tests"
4845
4946
5047# -------------------------------------------------------------------------------------------------
51- # Read-only Targets
48+ # Standard targets
5249# -------------------------------------------------------------------------------------------------
50+ gen : _pull-tfdocs
51+ @echo " ################################################################################"
52+ @echo " # Terraform-docs generate"
53+ @echo " ################################################################################"
54+ @$(MAKE ) --no-print-directory _gen-main
55+ @$(MAKE ) --no-print-directory _gen-examples
56+ @$(MAKE ) --no-print-directory _gen-modules
5357
5458lint :
55- @$(MAKE ) --no-print-directory terraform-fmt _WRITE=false
56- @$(MAKE ) --no-print-directory lint-files
59+ @if [ " $( LINT_FL_ENABLE) " = " 1" ]; then \
60+ $(MAKE ) --no-print-directory _lint-files; \
61+ fi
62+ @if [ " $( LINT_TF_ENABLE) " = " 1" ]; then \
63+ $(MAKE ) --no-print-directory _lint-fmt; \
64+ fi
65+ @if [ " $( LINT_JL_ENABLE) " = " 1" ]; then \
66+ $(MAKE ) --no-print-directory _lint-json; \
67+ fi
5768
58- lint-files :
59- @echo " ################################################################################"
60- @echo " # file-lint"
61- @echo " ################################################################################"
62- @docker run --rm -v $(PWD ) :/data cytopia/file-lint:$(FL_VERSION ) file-cr --text --ignore ' $(FL_IGNORE_PATHS)' --path .
63- @docker run --rm -v $(PWD ) :/data cytopia/file-lint:$(FL_VERSION ) file-crlf --text --ignore ' $(FL_IGNORE_PATHS)' --path .
64- @docker run --rm -v $(PWD ) :/data cytopia/file-lint:$(FL_VERSION ) file-trailing-single-newline --text --ignore ' $(FL_IGNORE_PATHS)' --path .
65- @docker run --rm -v $(PWD ) :/data cytopia/file-lint:$(FL_VERSION ) file-trailing-space --text --ignore ' $(FL_IGNORE_PATHS)' --path .
66- @docker run --rm -v $(PWD ) :/data cytopia/file-lint:$(FL_VERSION ) file-utf8 --text --ignore ' $(FL_IGNORE_PATHS)' --path .
67- @docker run --rm -v $(PWD ) :/data cytopia/file-lint:$(FL_VERSION ) file-utf8-bom --text --ignore ' $(FL_IGNORE_PATHS)' --path .
69+ test : _pull-tf
70+ @$(foreach example,\
71+ $(TF_EXAMPLES ) ,\
72+ DOCKER_PATH=" /t/examples/$( notdir $( patsubst %/,%,$( example) ) ) " ; \
73+ echo " ################################################################################" ; \
74+ echo " # examples/$$ ( basename $$ {DOCKER_PATH} )" ; \
75+ echo " ################################################################################" ; \
76+ echo ; \
77+ echo " ------------------------------------------------------------" ; \
78+ echo " # Terraform init" ; \
79+ echo " ------------------------------------------------------------" ; \
80+ if docker run $$ (tty -s && echo " -it" || echo) --rm -v " $( CURRENT_DIR) :/t" \
81+ --workdir " $$ {DOCKER_PATH}" --network host \
82+ hashicorp/terraform:$(TF_VERSION ) \
83+ init \
84+ -lock=false \
85+ -upgrade \
86+ -reconfigure \
87+ -input=false \
88+ -get=true; \
89+ then \
90+ echo " OK" ; \
91+ else \
92+ echo " Failed" ; \
93+ docker run $$(tty -s && echo "-it" || echo ) --rm -v " $( CURRENT_DIR) :/t" \
94+ --workdir " $$ {DOCKER_PATH}" --network none --entrypoint=rm \
95+ hashicorp/terraform:$(TF_VERSION ) -rf .terraform/ || true ; \
96+ exit 1; \
97+ fi ; \
98+ echo ; \
99+ echo " ------------------------------------------------------------" ; \
100+ echo " # Terraform validate" ; \
101+ echo " ------------------------------------------------------------" ; \
102+ if docker run $$ (tty -s && echo " -it" || echo) --rm -v " $( CURRENT_DIR) :/t" \
103+ --workdir " $$ {DOCKER_PATH}" --network host \
104+ hashicorp/terraform:$(TF_VERSION ) \
105+ validate \
106+ $(ARGS ) \
107+ . ; then \
108+ echo " OK" ; \
109+ docker run $$(tty -s && echo "-it" || echo ) --rm -v " $( CURRENT_DIR) :/t" \
110+ --workdir " $$ {DOCKER_PATH}" --network none --entrypoint=rm \
111+ hashicorp/terraform:$(TF_VERSION ) -rf .terraform/ || true ; \
112+ else \
113+ echo " Failed" ; \
114+ docker run $$(tty -s && echo "-it" || echo ) --rm -v " $( CURRENT_DIR) :/t" \
115+ --workdir " $$ {DOCKER_PATH}" --network none --entrypoint=rm \
116+ hashicorp/terraform:$(TF_VERSION ) -rf .terraform/ || true ; \
117+ exit 1; \
118+ fi ; \
119+ echo ; \
120+ )
68121
69122
70123# -------------------------------------------------------------------------------------------------
71- # Writing Targets
124+ # Helper Targets
72125# -------------------------------------------------------------------------------------------------
126+ _gen-main :
127+ @echo " ------------------------------------------------------------"
128+ @echo " # Main module"
129+ @echo " ------------------------------------------------------------"
130+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network none \
131+ -v $(CURRENT_DIR ) :/data \
132+ -e DELIM_START=' <!-- TFDOCS_HEADER_START -->' \
133+ -e DELIM_CLOSE=' <!-- TFDOCS_HEADER_END -->' \
134+ cytopia/terraform-docs:$(TFDOCS_VERSION ) \
135+ terraform-docs-replace --show header markdown tbl --indent 2 --sort README.md; then \
136+ echo " OK" ; \
137+ else \
138+ echo " Failed" ; \
139+ exit 1; \
140+ fi
141+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network none \
142+ -v $(CURRENT_DIR ) :/data \
143+ -e DELIM_START=' <!-- TFDOCS_PROVIDER_START -->' \
144+ -e DELIM_CLOSE=' <!-- TFDOCS_PROVIDER_END -->' \
145+ cytopia/terraform-docs:$(TFDOCS_VERSION ) \
146+ terraform-docs-replace --show providers markdown tbl --indent 2 --sort README.md; then \
147+ echo " OK" ; \
148+ else \
149+ echo " Failed" ; \
150+ exit 1; \
151+ fi
152+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network none \
153+ -v $(CURRENT_DIR ) :/data \
154+ -e DELIM_START=' <!-- TFDOCS_REQUIREMENTS_START -->' \
155+ -e DELIM_CLOSE=' <!-- TFDOCS_REQUIREMENTS_END -->' \
156+ cytopia/terraform-docs:$(TFDOCS_VERSION ) \
157+ terraform-docs-replace --show requirements markdown tbl --indent 2 --sort README.md; then \
158+ echo " OK" ; \
159+ else \
160+ echo " Failed" ; \
161+ exit 1; \
162+ fi
163+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network none \
164+ -v $(CURRENT_DIR ) :/data \
165+ -e DELIM_START=' <!-- TFDOCS_INPUTS_START -->' \
166+ -e DELIM_CLOSE=' <!-- TFDOCS_INPUTS_END -->' \
167+ cytopia/terraform-docs:$(TFDOCS_VERSION ) \
168+ terraform-docs-replace --show inputs markdown doc --indent 2 $(TFDOCS_ARGS ) README.md; then \
169+ echo " OK" ; \
170+ else \
171+ echo " Failed" ; \
172+ exit 1; \
173+ fi
174+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network none \
175+ -v $(CURRENT_DIR ) :/data \
176+ -e DELIM_START=' <!-- TFDOCS_OUTPUTS_START -->' \
177+ -e DELIM_CLOSE=' <!-- TFDOCS_OUTPUTS_END -->' \
178+ cytopia/terraform-docs:$(TFDOCS_VERSION ) \
179+ terraform-docs-replace --show outputs markdown tbl --indent 2 --sort README.md; then \
180+ echo " OK" ; \
181+ else \
182+ echo " Failed" ; \
183+ exit 1; \
184+ fi
185+
186+ _gen-examples :
187+ @$(foreach example,\
188+ $(TF_EXAMPLES ) ,\
189+ DOCKER_PATH=" examples/$( notdir $( patsubst %/,%,$( example) ) ) " ; \
190+ echo " ------------------------------------------------------------" ; \
191+ echo " # $$ {DOCKER_PATH}" ; \
192+ echo " ------------------------------------------------------------" ; \
193+ if docker run $$ (tty -s && echo " -it" || echo) --rm --network none \
194+ -v $(CURRENT_DIR ) :/data \
195+ -e DELIM_START=' $(DELIM_START)' \
196+ -e DELIM_CLOSE=' $(DELIM_CLOSE)' \
197+ cytopia/terraform-docs:$(TFDOCS_VERSION ) \
198+ terraform-docs-replace $(TFDOCS_ARGS ) markdown $$ {DOCKER_PATH}/README.md; then \
199+ echo " OK" ; \
200+ else \
201+ echo " Failed" ; \
202+ exit 1; \
203+ fi ; \
204+ )
205+
206+ _gen-modules :
207+ @$(foreach module,\
208+ $(TF_MODULES ) ,\
209+ DOCKER_PATH=" modules/$( notdir $( patsubst %/,%,$( module) ) ) " ; \
210+ echo " ------------------------------------------------------------" ; \
211+ echo " # $$ {DOCKER_PATH}" ; \
212+ echo " ------------------------------------------------------------" ; \
213+ if docker run $$ (tty -s && echo " -it" || echo) --rm --network none \
214+ -v $(CURRENT_DIR ) :/data \
215+ -e DELIM_START=' $(DELIM_START)' \
216+ -e DELIM_CLOSE=' $(DELIM_CLOSE)' \
217+ cytopia/terraform-docs:$(TFDOCS_VERSION ) \
218+ terraform-docs-replace $(TFDOCS_ARGS ) markdown $$ {DOCKER_PATH}/README.md; then \
219+ echo " OK" ; \
220+ else \
221+ echo " Failed" ; \
222+ exit 1; \
223+ fi ; \
224+ )
73225
74- terraform-docs : _pull-tfdocs
226+ _lint-files : _pull-fl
227+ @# Basic file linting
75228 @echo " ################################################################################"
76- @echo " # Terraform-docs generate "
229+ @echo " # File-lint "
77230 @echo " ################################################################################"
78- @echo
79- @if docker run --rm $$(tty -s && echo "-it" || echo ) \
80- -v "$(CURRENT_DIR ) :/data" \
81- -e TFDOCS_DELIM_START='$(TFDOCS_DELIM_START)' \
82- -e TFDOCS_DELIM_CLOSE='$(TFDOCS_DELIM_CLOSE)' \
83- cytopia/terraform-docs :$(TFDOCS_VERSION ) \
84- terraform-docs-replace --sort-by required markdown README.md; then \
85- echo "OK"; \
86- else \
87- echo "Failed"; \
88- exit 1; \
89- fi;
90- @echo
231+ @docker run $$(tty -s && echo "-it" || echo ) --rm --network none -v $(CURRENT_DIR ) :/data cytopia/file-lint:$(FL_VERSION ) file-cr --text --ignore ' $(FL_IGNORE_PATHS)' --path .
232+ @docker run $$(tty -s && echo "-it" || echo ) --rm --network none -v $(CURRENT_DIR ) :/data cytopia/file-lint:$(FL_VERSION ) file-crlf --text --ignore ' $(FL_IGNORE_PATHS)' --path .
233+ @docker run $$(tty -s && echo "-it" || echo ) --rm --network none -v $(CURRENT_DIR ) :/data cytopia/file-lint:$(FL_VERSION ) file-trailing-single-newline --text --ignore ' $(FL_IGNORE_PATHS)' --path .
234+ @docker run $$(tty -s && echo "-it" || echo ) --rm --network none -v $(CURRENT_DIR ) :/data cytopia/file-lint:$(FL_VERSION ) file-trailing-space --text --ignore ' $(FL_IGNORE_PATHS)' --path .
235+ @docker run $$(tty -s && echo "-it" || echo ) --rm --network none -v $(CURRENT_DIR ) :/data cytopia/file-lint:$(FL_VERSION ) file-utf8 --text --ignore ' $(FL_IGNORE_PATHS)' --path .
236+ @docker run $$(tty -s && echo "-it" || echo ) --rm --network none -v $(CURRENT_DIR ) :/data cytopia/file-lint:$(FL_VERSION ) file-utf8-bom --text --ignore ' $(FL_IGNORE_PATHS)' --path .
91237
92- terraform-fmt : _WRITE=true
93- terraform-fmt : _pull-tf
238+ _lint-fmt : _pull-tf
94239 @# Lint all Terraform files
95240 @echo " ################################################################################"
96241 @echo " # Terraform fmt"
@@ -99,13 +244,10 @@ terraform-fmt: _pull-tf
99244 @echo " ------------------------------------------------------------"
100245 @echo " # *.tf files"
101246 @echo " ------------------------------------------------------------"
102- @if docker run $$(tty -s && echo "-it" || echo ) --rm \
103- -v " $( PWD) :/data" hashicorp/terraform:$(TF_VERSION ) fmt \
104- $$(test "$(_WRITE ) " = "false" && echo "-check" || echo "-write=true" ) \
105- -diff \
106- -list=true \
107- -recursive \
108- /data; then \
247+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network host \
248+ -v " $( CURRENT_DIR) :/t:ro" --workdir " /t" \
249+ hashicorp/terraform:$(TF_VERSION ) \
250+ fmt -recursive -check=true -diff=true -write=true -list=true . ; then \
109251 echo " OK" ; \
110252 else \
111253 echo " Failed" ; \
@@ -115,29 +257,38 @@ terraform-fmt: _pull-tf
115257 @echo " ------------------------------------------------------------"
116258 @echo " # *.tfvars files"
117259 @echo " ------------------------------------------------------------"
118- @if docker run $$(tty -s && echo "-it" || echo ) --rm --entrypoint=/bin/sh \
119- -v " $( PWD) :/data" hashicorp/terraform:$(TF_VERSION ) \
120- -c " find . -not \( -path './*/.terragrunt-cache/*' -o -path './*/.terraform/*' \) \
121- -name ' *.tfvars' -type f -print0 \
122- | xargs -0 -n1 terraform fmt \
123- $$(test '$(_WRITE ) ' = 'false' && echo '-check' || echo '-write=true' ) \
124- -diff \
125- -list=true" ; then \
260+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network host \
261+ --entrypoint=/bin/sh -v " $( CURRENT_DIR) :/t:ro" --workdir " /t" \
262+ hashicorp/terraform:$(TF_VERSION ) \
263+ -c " find . -name '*.tfvars' -type f -print0 | xargs -0 -n1 terraform fmt -check=true -write=true -diff=true -list=true" ; then \
126264 echo " OK" ; \
127265 else \
128266 echo " Failed" ; \
129267 exit 1; \
130268 fi ;
131269 @echo
132270
271+ _lint-json : _pull-jl
272+ @# Lint all JSON files
273+ @echo " ################################################################################"
274+ @echo " # Jsonlint"
275+ @echo " ################################################################################"
276+ @if docker run $$(tty -s && echo "-it" || echo ) --rm --network none \
277+ -v " $( CURRENT_DIR) :/data:ro" \
278+ cytopia/jsonlint:$(JL_VERSION ) \
279+ -t ' ' -i ' *.terraform/*' ' *.json' ; then \
280+ echo " OK" ; \
281+ else \
282+ echo " Failed" ; \
283+ exit 1; \
284+ fi ;
285+ @echo
133286
134- # -------------------------------------------------------------------------------------------------
135- # Helper Targets
136- # -------------------------------------------------------------------------------------------------
137-
138- # Ensure to always have the latest Terraform version
139287_pull-tf :
140288 docker pull hashicorp/terraform:$(TF_VERSION )
141289
142290_pull-tfdocs :
143291 docker pull cytopia/terraform-docs:$(TFDOCS_VERSION )
292+
293+ _pull-fl :
294+ docker pull cytopia/file-lint:$(FL_VERSION )
0 commit comments