44[ ![ Tag] ( https://img.shields.io/github/tag/cytopia/docker-terraform-docs.svg )] ( https://github.com/cytopia/docker-terraform-docs/releases )
55[ ![ ] ( https://images.microbadger.com/badges/version/cytopia/terraform-docs:latest.svg )] ( https://microbadger.com/images/cytopia/terraform-docs:latest " terraform-docs ")
66[ ![ ] ( https://images.microbadger.com/badges/image/cytopia/terraform-docs:latest.svg )] ( https://microbadger.com/images/cytopia/terraform-docs:latest " terraform-docs ")
7+ [ ![ ] ( https://img.shields.io/badge/github-cytopia%2Fdocker--terraform--docs-red.svg )] ( https://github.com/cytopia/docker-terraform-docs " github.com/cytopia/docker-terraform-docs ")
78[ ![ License] ( https://img.shields.io/badge/license-MIT-%233DA639.svg )] ( https://opensource.org/licenses/MIT )
89
910
1011[ ![ Docker hub] ( http://dockeri.co/image/cytopia/terraform-docs )] ( https://hub.docker.com/r/cytopia/terraform-docs )
1112
1213
13- ## Official project
14+ Dockerized version of [ terraform-docs] ( https://github.com/segmentio/terraform-docs ) <sup >[ 1] </sup >,
15+ which additionally implements ` terraform-docs-replace ` allowing you to automatically and safely
16+ replace the ` terraform-docs ` generated output infile.
1417
15- https://github.com/segmentio/terraform-docs
18+ < sub > [ 1 ] Official project: https://github.com/segmentio/terraform-docs </ sub >
1619
1720
1821## Available Docker image versions
@@ -30,6 +33,17 @@ https://github.com/segmentio/terraform-docs
3033| ` 0.1.0 ` | [ Tag: v0.1.0] ( https://github.com/segmentio/terraform-docs/tree/v0.1.0 ) |
3134
3235
36+ ## Environment variables
37+
38+ The following Docker environment variables are available. These will only need to be used when
39+ using ` terraform-docs-replace ` .
40+
41+ | Variable | Default | Required | Comment |
42+ | ----------| ---------| ----------| ---------|
43+ | DELIM_START | ` <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> ` | No | The starting delimiter in the file in where you want to replace the ` terraform-docs ` output. |
44+ | DELIM_CLOSE | ` <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> ` | No | The ending delimiter in the file in where you want to replace the ` terraform-docs ` output. |
45+
46+
3347## Docker mounts
3448
3549The working directory inside the Docker container is ` /docs/ ` and should be mounted locally to
@@ -43,21 +57,25 @@ Create markdown output and sent to stdout:
4357``` bash
4458docker run --rm \
4559 -v $( pwd) :/docs \
46- --sort-inputs-by-required --with-aggregate-type-defaults md .
60+ cytopia/terraform-docs \
61+ --sort-inputs-by-required terraform-docs --with-aggregate-type-defaults md .
4762```
4863
4964#### Store in file
5065Create README.md with ` terraform-docs ` output:
5166``` bash
5267docker run --rm \
5368 -v $( pwd) :/docs \
54- --sort-inputs-by-required --with-aggregate-type-defaults md . > README.md
69+ cytopia/terraform-docs \
70+ terraform-docs --sort-inputs-by-required --with-aggregate-type-defaults md . > README.md
5571```
5672
5773#### Replace in README.md
5874Replace current ` terraform-docs ` blocks in README.md with current one in order to automatically
5975keep it up to date. For this to work, the ` terraform-docs ` information must be wrapped with the
60- following delimiter:
76+ following delimiter by default:
77+
78+ ` README.md: `
6179``` markdown
6280<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6381## Inputs
@@ -66,20 +84,130 @@ following delimiter:
6684```
6785
6886``` bash
69- # Save output in variable
70- DOCS=" $(
71- docker run --rm \
72- -v $( pwd) :/docs \
73- --sort-inputs-by-required --with-aggregate-type-defaults md .
74- ) "
75-
76- # Create new README
77- grep -B 100000000 -F ' <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->' README.md > README.md.tmp
78- printf " ${DOCS} \n\n" >> README.md.tmp
79- grep -A 100000000 -F ' <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->' README.md >> README.md.tmp
80-
81- # Overwrite old README
82- mv -f README.md.tmp README.md
87+ # Path to README.md must be specified as last command.
88+ # Note that the command changes from terraform-docs to terraform-docs-replace
89+ docker run --rm \
90+ -v $( pwd) :/docs \
91+ cytopia/terraform-docs \
92+ terraform-docs-replace --sort-inputs-by-required --with-aggregate-type-defaults md README.md
93+ ```
94+
95+ #### Replace in INFO.md with different delimiter
96+ You are able to use different delimiter. Let's imagine the following delimiter:
97+
98+ ` INFO.md: `
99+ ``` markdown
100+ <!-- TFDOC_START -->
101+ ## Inputs
102+ ...
103+ <!-- TFDOC_END -->
104+ ```
105+
106+ ``` bash
107+ # Path to INFO.md must be specified as last command.
108+ # Note that the command changes from terraform-docs to terraform-docs-replace
109+ docker run --rm \
110+ -v $( pwd) :/docs \
111+ -e DELIM_START=' TFDOC_START' \
112+ -e DELIM_CLOSE=' TFDOC_END' \
113+ cytopia/terraform-docs \
114+ terraform-docs-replace --sort-inputs-by-required --with-aggregate-type-defaults md INFO.md
115+ ```
116+
117+ #### Example Makefile
118+ You can add the following Makefile to your project for easy generation of terraform-docs output in
119+ a Terraform module. It takes into consideration the Main module, sub-modules and examples.
120+
121+ ``` make
122+ .PHONY : gen _gen-main _gen-examples _gen-modules
123+
124+ CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
125+ TF_EXAMPLES = $(sort $(dir $(wildcard $(CURRENT_DIR ) examples/* /) ) )
126+ TF_MODULES = $(sort $(dir $(wildcard $(CURRENT_DIR ) modules/* /) ) )
127+ TF_DOCS_VERSION = 0.6.0
128+
129+ # Adjust your delimiter here or overwrite via make arguments
130+ DELIM_START = <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
131+ DELIM_CLOSE = <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
132+
133+ gen :
134+ @echo " ################################################################################"
135+ @echo " # Terraform-docs generate"
136+ @echo " ################################################################################"
137+ @$(MAKE ) _gen-main
138+ @$(MAKE ) _gen-examples
139+ @$(MAKE ) _gen-modules
140+
141+ _gen-main :
142+ @echo " ------------------------------------------------------------"
143+ @echo " # Main module"
144+ @echo " ------------------------------------------------------------"
145+ @if docker run --rm \
146+ -v $(CURRENT_DIR ) :/docs \
147+ -e DELIM_START=' $(DELIM_START)' \
148+ -e DELIM_CLOSE=' $(DELIM_CLOSE)' \
149+ cytopia/terraform-docs:${TF_DOCS_VERSION} \
150+ terraform-docs-replace --sort-inputs-by-required --with-aggregate-type-defaults md README.md; then \
151+ echo " OK" ; \
152+ else \
153+ echo " Failed" ; \
154+ exit 1; \
155+ fi
156+
157+ _gen-examples :
158+ @$(foreach example,\
159+ $(TF_EXAMPLES ) ,\
160+ DOCKER_PATH=" examples/$( notdir $( patsubst %/,%,$( example) ) ) " ; \
161+ echo " ------------------------------------------------------------" ; \
162+ echo " # $$ {DOCKER_PATH}" ; \
163+ echo " ------------------------------------------------------------" ; \
164+ if docker run --rm \
165+ -v $(CURRENT_DIR ) :/docs \
166+ -e DELIM_START=' $(DELIM_START)' \
167+ -e DELIM_CLOSE=' $(DELIM_CLOSE)' \
168+ cytopia/terraform-docs:${TF_DOCS_VERSION} \
169+ terraform-docs-replace --sort-inputs-by-required --with-aggregate-type-defaults md $$ {DOCKER_PATH}/README.md; then \
170+ echo " OK" ; \
171+ else \
172+ echo " Failed" ; \
173+ exit 1; \
174+ fi ; \
175+ )
176+
177+ _gen-modules :
178+ @$(foreach module,\
179+ $(TF_MODULES ) ,\
180+ DOCKER_PATH=" modules/$( notdir $( patsubst %/,%,$( module) ) ) " ; \
181+ echo " ------------------------------------------------------------" ; \
182+ echo " # $$ {DOCKER_PATH}" ; \
183+ echo " ------------------------------------------------------------" ; \
184+ if docker run --rm \
185+ -v $(CURRENT_DIR ) :/docs \
186+ -e DELIM_START=' $(DELIM_START)' \
187+ -e DELIM_CLOSE=' $(DELIM_CLOSE)' \
188+ cytopia/terraform-docs:${TF_DOCS_VERSION} \
189+ terraform-docs-replace --sort-inputs-by-required --with-aggregate-type-defaults md $$ {DOCKER_PATH}/README.md; then \
190+ echo " OK" ; \
191+ else \
192+ echo " Failed" ; \
193+ exit 1; \
194+ fi ; \
195+ )
196+ ```
197+
198+ # ### Travis CI integration
199+ With the above Makefile in place, you can easily add a Travis CI rule to ensure the terraform-docs
200+ output is always up-to-date and will fail otherwise (due to git changes) :
201+ ```yml
202+ ---
203+ sudo : required
204+ services :
205+ - docker
206+ before_install : true
207+ install : true
208+ script :
209+ - make gen
210+ - git diff --quiet || { echo "Build Changes"; git diff; git status; false; }
83211```
84212
85213
0 commit comments