Skip to content

Commit 073c782

Browse files
authored
Merge pull request #7 from cytopia/release-0.4
Terraform >= 0.12 ready
2 parents a877f39 + dbbbc35 commit 073c782

File tree

4 files changed

+138
-31
lines changed

4 files changed

+138
-31
lines changed

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ LABEL \
4444
repo="https://github.com/cytopia/docker-terraform-docs"
4545
COPY --from=builder /go/src/github.com/segmentio/terraform-docs/bin/linux-amd64/terraform-docs /usr/local/bin/terraform-docs
4646
COPY ./data/docker-entrypoint.sh /docker-entrypoint.sh
47+
COPY ./data/terraform-docs.awk /terraform-docs.awk
4748

48-
ENV WORKDIR /docs
49-
WORKDIR /docs
49+
ENV WORKDIR /data
50+
WORKDIR /data
5051

5152
CMD ["terraform-docs", "--version"]
5253
ENTRYPOINT ["/docker-entrypoint.sh"]

README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ View **[Dockerfile](https://github.com/cytopia/docker-terraform-docs/blob/master
2525
Tiny Alpine-based multistage-build dockerized version of [terraform-docs](https://github.com/segmentio/terraform-docs)<sup>[1]</sup>,
2626
which additionally implements `terraform-docs-replace` allowing you to automatically and safely
2727
replace the `terraform-docs` generated output infile.
28+
Furthermore this implementation is also **Terraform >= 0.12 ready**. See [Generic Usage](#generic) for more details.
2829
The image is built nightly against multiple stable versions and pushed to Dockerhub.
2930

3031
<sub>[1] Official project: https://github.com/segmentio/terraform-docs</sub>
@@ -58,28 +59,35 @@ using `terraform-docs-replace`.
5859

5960
## Docker mounts
6061

61-
The working directory inside the Docker container is `/docs/` and should be mounted locally to
62+
The working directory inside the Docker container is `/data/` and should be mounted locally to
6263
where your Terraform module is located.
6364

6465

6566
## Usage
6667

6768
#### Generic
6869
```bash
69-
Usage: docker run cytopia/terraform-docs terraform-docs <ARGS> .
70-
docker run cytopia/terraform-docs terraform-docs-replace <ARGS> <PATH-TO-FILE>
70+
Usage: cytopia/terraform-docs terraform-docs <ARGS> .
71+
cytopia/terraform-docs terraform-docs-012 <ARGS> .
7172

72-
terraform-docs Output as expected from terraform-docs
73-
terraform-docs-replace Same as above, but replaces directly inside README.md
74-
if DELIM_START and DELIM_CLOSE are found.
75-
<ARGS> All arguments terraform-docs command can use.
73+
cytopia/terraform-docs terraform-docs-replace <ARGS> <PATH-TO-FILE>
74+
cytopia/terraform-docs terraform-docs-replace-012 <ARGS> <PATH-TO-FILE>
75+
76+
77+
terraform-docs Output as expected from terraform-docs
78+
terraform-docs-012 Same as above, but used for Terraform >= 0.12 modules
79+
80+
terraform-docs-replace Replaces directly inside README.md, if DELIM_START and DELIM_CLOSE are found.
81+
terraform-docs-replace-012 Same as above, but used for Terraform >= 0.12 modules
82+
83+
<ARGS> All arguments terraform-docs command can use.
7684
```
7785
7886
#### Output to stdout
7987
Create markdown output and sent to stdout:
8088
```bash
8189
docker run --rm \
82-
-v $(pwd):/docs \
90+
-v $(pwd):/data \
8391
cytopia/terraform-docs \
8492
--sort-inputs-by-required terraform-docs --with-aggregate-type-defaults md .
8593
```
@@ -88,7 +96,7 @@ docker run --rm \
8896
Create README.md with `terraform-docs` output:
8997
```bash
9098
docker run --rm \
91-
-v $(pwd):/docs \
99+
-v $(pwd):/data \
92100
cytopia/terraform-docs \
93101
terraform-docs --sort-inputs-by-required --with-aggregate-type-defaults md . > README.md
94102
```
@@ -110,7 +118,7 @@ following delimiter by default:
110118
# Path to README.md must be specified as last command.
111119
# Note that the command changes from terraform-docs to terraform-docs-replace
112120
docker run --rm \
113-
-v $(pwd):/docs \
121+
-v $(pwd):/data \
114122
cytopia/terraform-docs \
115123
terraform-docs-replace --sort-inputs-by-required --with-aggregate-type-defaults md README.md
116124
```
@@ -130,7 +138,7 @@ You are able to use different delimiter. Let's imagine the following delimiter:
130138
# Path to INFO.md must be specified as last command.
131139
# Note that the command changes from terraform-docs to terraform-docs-replace
132140
docker run --rm \
133-
-v $(pwd):/docs \
141+
-v $(pwd):/data \
134142
-e DELIM_START='TFDOC_START' \
135143
-e DELIM_CLOSE='TFDOC_END' \
136144
cytopia/terraform-docs \
@@ -166,7 +174,7 @@ _gen-main:
166174
@echo "# Main module"
167175
@echo "------------------------------------------------------------"
168176
@if docker run --rm \
169-
-v $(CURRENT_DIR):/docs \
177+
-v $(CURRENT_DIR):/data \
170178
-e DELIM_START='$(DELIM_START)' \
171179
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
172180
cytopia/terraform-docs:${TF_DOCS_VERSION} \
@@ -185,7 +193,7 @@ _gen-examples:
185193
echo "# $${DOCKER_PATH}"; \
186194
echo "------------------------------------------------------------"; \
187195
if docker run --rm \
188-
-v $(CURRENT_DIR):/docs \
196+
-v $(CURRENT_DIR):/data \
189197
-e DELIM_START='$(DELIM_START)' \
190198
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
191199
cytopia/terraform-docs:${TF_DOCS_VERSION} \
@@ -205,7 +213,7 @@ _gen-modules:
205213
echo "# $${DOCKER_PATH}"; \
206214
echo "------------------------------------------------------------"; \
207215
if docker run --rm \
208-
-v $(CURRENT_DIR):/docs \
216+
-v $(CURRENT_DIR):/data \
209217
-e DELIM_START='$(DELIM_START)' \
210218
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
211219
cytopia/terraform-docs:${TF_DOCS_VERSION} \

data/docker-entrypoint.sh

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
set -e
44
set -u
55

6+
67
###
78
### Default variables
89
###
@@ -41,6 +42,25 @@ trim_last_arg() {
4142
}
4243

4344

45+
print_usage() {
46+
>&2 echo "Error, Unsupported command."
47+
>&2 echo "Usage: cytopia/terraform-docs terraform-docs <ARGS> ."
48+
>&2 echo " cytopia/terraform-docs terraform-docs-012 <ARGS> ."
49+
>&2 echo
50+
>&2 echo " cytopia/terraform-docs terraform-docs-replace <ARGS> <PATH-TO-FILE>"
51+
>&2 echo " cytopia/terraform-docs terraform-docs-replace-012 <ARGS> <PATH-TO-FILE>"
52+
>&2 echo
53+
>&2 echo
54+
>&2 echo "terraform-docs Output as expected from terraform-docs"
55+
>&2 echo "terraform-docs-012 Same as above, but used for Terraform >= 0.12 modules"
56+
>&2 echo
57+
>&2 echo "terraform-docs-replace Replaces directly inside README.md, if DELIM_START and DELIM_CLOSE are found."
58+
>&2 echo "terraform-docs-replace-012 Same as above, but used for Terraform >= 0.12 modules"
59+
>&2 echo
60+
>&2 echo "<ARGS> All arguments terraform-docs command can use."
61+
}
62+
63+
4464
###
4565
### Arguments appended?
4666
###
@@ -49,10 +69,7 @@ if [ "${#}" -ge "1" ]; then
4969
###
5070
### Custom replace operation
5171
###
52-
if [ "${1}" = "terraform-docs-replace" ]; then
53-
54-
# Remove first argument "replace"
55-
shift;
72+
if [ "${1}" = "terraform-docs-replace" ] || [ "${1}" = "terraform-docs-replace-012" ]; then
5673

5774
# Store and Remove last argument (filename)
5875
eval MY_FILE="\${$#}" # store last argument
@@ -81,9 +98,26 @@ if [ "${#}" -ge "1" ]; then
8198
GID="$(stat -c %g "${WORKDIR}/${MY_FILE}")"
8299
PERM="$(stat -c %a "${WORKDIR}/${MY_FILE}")"
83100

84-
# Get terraform-docs output
85-
>&2 echo "terraform-docs ${*} ${WORKDIR}/$(dirname ${MY_FILE})"
86-
DOCS="$(terraform-docs "${@}" "${WORKDIR}/$(dirname ${MY_FILE})")"
101+
# Terraform < 0.12
102+
if [ "${1}" = "terraform-docs-replace" ]; then
103+
# Remove first argument "replace"
104+
shift;
105+
# Get terraform-docs output
106+
>&2 echo "terraform-docs ${*} ${WORKDIR}"
107+
DOCS="$(terraform-docs "${@}" "${WORKDIR}")"
108+
# Terraform >= 0.12
109+
else
110+
# Remove first argument "replace"
111+
shift;
112+
mkdir -p /tmp-012
113+
awk -f /terraform-docs.awk *.tf > "/tmp-012/tmp.tf"
114+
# Get terraform-docs output
115+
>&2 echo "terraform-docs-012 ${*} ${WORKDIR}"
116+
if ! DOCS="$(terraform-docs "${@}" "/tmp-012")"; then
117+
cat -n /tmp-012/tmp.tf
118+
exit 1
119+
fi
120+
fi
87121

88122
# Create temporary README.md
89123
mkdir -p /tmp
@@ -100,22 +134,31 @@ if [ "${#}" -ge "1" ]; then
100134
exit 0
101135

102136
###
103-
### terraform-docs command
137+
### terraform-docs command (< 0.12)
104138
###
105139
elif [ "${1}" = "terraform-docs" ]; then
106140
exec "${@}"
107141

142+
###
143+
### terraform-docs command (>= 0.12)
144+
###
145+
elif [ "${1}" = "terraform-docs-012" ]; then
146+
mkdir -p /tmp-012
147+
awk -f /usr/bin/terraform-docs.awk *.tf > "/tmp-012/tmp.tf"
148+
149+
# Remove last argument (path)
150+
args="$(trim_last_arg "${@}")" # get all the args except the last arg
151+
eval "set -- ${args}" # update the shell's arguments with the new value
152+
# Remove first argument (terraform-docs-012)
153+
shift
154+
# Execute
155+
exec terraform-docs "${@}" "/tmp-012/"
156+
108157
###
109158
### Unsupported command
110159
###
111160
else
112-
>&2 echo "Error, Unsupported command."
113-
>&2 echo "Usage: cytopia/terraform-docs terraform-docs <ARGS> ."
114-
>&2 echo " cytopia/terraform-docs terraform-docs-replace <ARGS> <PATH-TO-FILE>"
115-
>&2 echo
116-
>&2 echo "terraform-docs Output as expected from terraform-docs"
117-
>&2 echo "terraform-docs-replace Same as above, but replaces directly inside README.md"
118-
>&2 echo " if DELIM_START and DELIM_CLOSE are found."
161+
print_usage
119162
exit 1
120163

121164
fi

data/terraform-docs.awk

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
2+
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
3+
# https://github.com/segmentio/terraform-docs/
4+
# https://github.com/segmentio/terraform-docs/issues/62
5+
6+
# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk
7+
8+
{
9+
if ( /\{/ ) {
10+
braceCnt++
11+
}
12+
13+
if ( /\}/ ) {
14+
braceCnt--
15+
}
16+
17+
if ($0 ~ /(variable|output) "(.*?)"/) {
18+
blockCnt++
19+
print $0
20+
}
21+
22+
if (blockCnt > 0) {
23+
if ($1 == "description") {
24+
print $0
25+
}
26+
}
27+
28+
if (blockCnt > 0) {
29+
if ($1 == "default") {
30+
if (braceCnt > 1) {
31+
print " default = {}"
32+
} else {
33+
print $0
34+
}
35+
}
36+
}
37+
38+
if (blockCnt > 0) {
39+
if ($1 == "type" ) {
40+
type=$3
41+
if (type ~ "object") {
42+
print " type = \"object\""
43+
} else {
44+
print " type = \"" $3 "\""
45+
}
46+
}
47+
}
48+
49+
if (blockCnt > 0) {
50+
if (braceCnt == 0 && blockCnt > 0) {
51+
blockCnt--
52+
print $0
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)