-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdocker-entrypoint-0.12.sh
executable file
·148 lines (121 loc) · 4.01 KB
/
docker-entrypoint-0.12.sh
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
144
145
146
147
148
#!/bin/sh
set -e
set -u
###
### Default variables
###
DEF_DELIM_START='<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->'
DEF_DELIM_CLOSE='<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->'
###
### Environment variables
###
# Set delimiter
if ! env | grep -q '^DELIM_START='; then
DELIM_START="${DEF_DELIM_START}"
fi
if ! env | grep -q '^DELIM_CLOSE='; then
DELIM_CLOSE="${DEF_DELIM_CLOSE}"
fi
###
### Helper functions
###
# Returns all but the last argument as an array using a POSIX-compliant method
# for handling arrays.
# Credit: https://gist.github.com/akutz/7a39159bbbe9c299c79f1d2107ef1357
trim_last_arg() {
_l="${#}" _i=0 _j="$((_l-1))" && while [ "${_i}" -lt "${_l}" ]; do
if [ "${_i}" -lt "${_j}" ]; then
printf '%s\n' "${1}" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/"
fi
shift; _i="$((_i+1))"
done
echo " "
}
print_usage() {
>&2 echo "Error, Unsupported command."
>&2 echo "Usage: cytopia/terraform-docs terraform-docs <ARGS> ."
>&2 echo " cytopia/terraform-docs terraform-docs-012 <ARGS> ."
>&2 echo
>&2 echo " cytopia/terraform-docs terraform-docs-replace <ARGS> <PATH-TO-FILE>"
>&2 echo " cytopia/terraform-docs terraform-docs-replace-012 <ARGS> <PATH-TO-FILE>"
>&2 echo
>&2 echo
>&2 echo "terraform-docs Output as expected from terraform-docs"
>&2 echo "terraform-docs-012 Same as above, but used for Terraform >= 0.12 modules"
>&2 echo " (Not required anymore and will become deprecated)"
>&2 echo
>&2 echo "terraform-docs-replace Replaces directly inside README.md, if DELIM_START and DELIM_CLOSE are found."
>&2 echo "terraform-docs-replace-012 Same as above, but used for Terraform >= 0.12 modules"
>&2 echo " (Not required anymore and will become deprecated)"
>&2 echo
>&2 echo "<ARGS> All arguments terraform-docs command can use."
>&2 echo "<PATH-TO-FILE> File in where to auto-replace terraform-docs block."
}
###
### Arguments appended?
###
if [ "${#}" -ge "1" ]; then
###
### Custom replace operation
###
if [ "${1}" = "terraform-docs-replace" ] || [ "${1}" = "terraform-docs-replace-012" ]; then
# Store and Remove last argument (filename)
eval MY_FILE="\${$#}" # store last argument
args="$(trim_last_arg "${@}")" # get all the args except the last arg
eval "set -- ${args}" # update the shell's arguments with the new value
# Check if file exists
if [ ! -f "${MY_FILE}" ]; then
>&2 echo "Error, File not found in: ${MY_FILE}"
exit 1
fi
# Check if starting delimiter exists in file
if ! grep -Fq "${DELIM_START}" "${MY_FILE}"; then
>&2 echo "Error, Starting delimiter not found ${MY_FILE}: '${DELIM_START}'"
exit 1
fi
# Check if closint delimiter exists in file
if ! grep -Fq "${DELIM_CLOSE}" "${MY_FILE}"; then
>&2 echo "Error, Closing delimiter not found ${MY_FILE}: '${DELIM_CLOSE}'"
exit 1
fi
# Get owner and permissions of current file
UID="$(stat -c %u "${MY_FILE}")"
GID="$(stat -c %g "${MY_FILE}")"
PERM="$(stat -c %a "${MY_FILE}")"
# Remove first argument "replace"
shift;
# Get terraform-docs output
>&2 echo "terraform-docs ${*} $(dirname "${MY_FILE}")"
DOCS="$(terraform-docs "${@}" "$(dirname "${MY_FILE}")")"
# Create temporary README.md
mkdir -p /tmp
grep -B 100000000 -F "${DELIM_START}" "${MY_FILE}" > /tmp/README.md
printf "%s\\n\\n" "${DOCS}" >> /tmp/README.md
grep -A 100000000 -F "${DELIM_CLOSE}" "${MY_FILE}" >> /tmp/README.md
# Adjust permissions of temporary file
chown "${UID}:${GID}" /tmp/README.md
chmod "${PERM}" /tmp/README.md
# Overwrite existing file
mv -f /tmp/README.md "${MY_FILE}"
exit 0
###
### terraform-docs command
###
elif [ "${1}" = "terraform-docs" ] || [ "${1}" = "terraform-docs-012" ]; then
# Remove first argument "replace"
shift
>&2 echo "terraform-docs ${*}"
terraform-docs "${@}"
###
### Unsupported command
###
else
print_usage
exit 1
fi
###
### No arguments appended
###
else
exec terraform-docs --version
fi