Skip to content

Commit aeef68c

Browse files
committed
Use native output file, method, and template
Signed-off-by: Khosrow Moossavi <[email protected]>
1 parent 3a63a94 commit aeef68c

File tree

1 file changed

+47
-73
lines changed

1 file changed

+47
-73
lines changed

src/docker-entrypoint.sh

Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
set -e
17+
set -o errexit
18+
set -o pipefail
19+
set -o errtrace
1820

1921
# Ensure all variables are present
2022
WORKING_DIR="${1}"
@@ -71,9 +73,9 @@ git_add() {
7173
file="$1"
7274
git add "${file}"
7375
if [ "$(git status --porcelain | grep "$file" | grep -c -E '([MA]\W).+')" -eq 1 ]; then
74-
echo "::debug file=entrypoint.sh,line=46 Added ${file} to git staging area"
76+
echo "::debug Added ${file} to git staging area"
7577
else
76-
echo "::debug file=entrypoint.sh,line=48 No change in ${file} detected"
78+
echo "::debug No change in ${file} detected"
7779
fi
7880
}
7981

@@ -82,95 +84,69 @@ git_status() {
8284
}
8385

8486
git_commit() {
85-
local is_clean
86-
set +e
87-
is_clean=$(git_status)
88-
set -e
89-
if [ "${is_clean}" -eq 0 ]; then
90-
echo "::debug file=entrypoint.sh,line=54 No files changed, skipping commit"
87+
if [ "$(git_status)" -eq 0 ]; then
88+
echo "::debug No files changed, skipping commit"
9189
exit 0
92-
else
93-
local signoff
94-
signoff=""
95-
if [ "${GIT_PUSH_SIGN_OFF}" = "true" ]; then
96-
signoff="-s"
97-
fi
98-
git commit ${signoff} -m "${GIT_COMMIT_MESSAGE}"
9990
fi
91+
92+
local signoff
93+
if [ "${GIT_PUSH_SIGN_OFF}" = "true" ]; then
94+
signoff="-s"
95+
fi
96+
git commit ${signoff} -m "${GIT_COMMIT_MESSAGE}"
10097
}
10198

10299
update_doc() {
103100
local working_dir
104-
local generated
105-
local success
106101

107102
working_dir="$1"
108-
echo "::debug file=entrypoint.sh,line=66 working_dir=${working_dir}"
103+
echo "::debug working_dir=${working_dir}"
109104

110-
set +e
105+
local config_file
111106

112-
# shellcheck disable=SC2086
113107
if [ -n "${CONFIG_FILE}" ] && [ "${CONFIG_FILE}" != "disabled" ]; then
114-
echo "::debug file=entrypoint.sh,line=80 command=terraform-docs --config ${CONFIG_FILE} ${ARGS} ${working_dir}"
115-
local config_file
116108
if [ -f "${CONFIG_FILE}" ]; then
117109
config_file="${CONFIG_FILE}"
118110
else
119111
config_file="${working_dir}/${CONFIG_FILE}"
120112
fi
121-
terraform-docs --config ${config_file} ${ARGS} ${working_dir} >/tmp/tf_generated
122-
success=$?
123-
else
124-
echo "::debug file=entrypoint.sh,line=84 command=terraform-docs ${OUTPUT_FORMAT} ${ARGS} ${working_dir}"
125-
terraform-docs ${OUTPUT_FORMAT} ${ARGS} ${working_dir} >/tmp/tf_generated
126-
success=$?
127-
fi
128-
129-
set -e
130113

131-
if [ $success -ne 0 ]; then
132-
echo "::error file=entrypoint.sh,line=89::$(cat /tmp/tf_generated)"
133-
rm -f /tmp/tf_generated
134-
exit $success
114+
echo "::debug config_file=${config_file}"
115+
config_file="--config ${config_file}"
135116
fi
136117

137-
generated=$(cat /tmp/tf_generated)
138-
rm -f /tmp/tf_generated
118+
local output_mode
119+
local output_file
139120

140-
case "${OUTPUT_METHOD}" in
141-
print)
142-
echo "${generated}"
143-
;;
121+
if [ "${OUTPUT_METHOD}" == "inject" ] || [ "${OUTPUT_METHOD}" == "replace" ]; then
122+
echo "::debug output_mode=${OUTPUT_METHOD}"
123+
output_mode="--output-mode ${OUTPUT_METHOD}"
144124

145-
replace)
146-
echo "${generated}" >"${working_dir}/${OUTPUT_FILE}"
147-
git_add "${working_dir}/${OUTPUT_FILE}"
148-
;;
125+
echo "::debug output_file=${OUTPUT_FILE}"
126+
output_file="--output-file ${OUTPUT_FILE}"
127+
fi
149128

150-
inject)
151-
# Create file if it doesn't exist
152-
if [ ! -f "${working_dir}/${OUTPUT_FILE}" ]; then
153-
echo "${TEMPLATE}" >"${working_dir}/${OUTPUT_FILE}"
154-
fi
129+
local success
155130

156-
local has_delimiter
157-
has_delimiter=$(grep -c -E '(BEGIN|END)_TF_DOCS' "${working_dir}/${OUTPUT_FILE}")
158-
echo "::debug file=entrypoint.sh,line=115 has_delimiter=${has_delimiter}"
131+
echo "::debug terraform-docs ${config_file} ${OUTPUT_FORMAT} ${ARGS} ${output_mode} ${output_file} ${working_dir}"
132+
# shellcheck disable=SC2086
133+
terraform-docs \
134+
${config_file} \
135+
${OUTPUT_FORMAT} \
136+
${ARGS} \
137+
${output_mode} \
138+
${output_file} \
139+
${working_dir}
140+
success=$?
159141

160-
# Verify it has BEGIN and END markers
161-
if [ "${has_delimiter}" -ne 2 ]; then
162-
echo "::error file=entrypoint.sh,line=119::Output file ${working_dir}/${OUTPUT_FILE} does not contain BEGIN_TF_DOCS and END_TF_DOCS"
163-
exit 1
164-
fi
142+
if [ $success -ne 0 ]; then
143+
exit $success
144+
fi
165145

166-
# Output generated markdown to temporary file with a trailing newline and then replace the block
167-
echo "${generated}" >/tmp/tf_doc.md
168-
echo "" >>/tmp/tf_doc.md
169-
sed -i -ne '/<!--- BEGIN_TF_DOCS --->/ {p; r /tmp/tf_doc.md' -e ':a; n; /<!--- END_TF_DOCS --->/ {p; b}; ba}; p' "${working_dir}/${OUTPUT_FILE}"
146+
if [ "${OUTPUT_METHOD}" == "inject" ] || [ "${OUTPUT_METHOD}" == "replace" ]; then
170147
git_add "${working_dir}/${OUTPUT_FILE}"
171-
rm -f /tmp/tf_doc.md
172-
;;
173-
esac
148+
fi
149+
174150
}
175151

176152
# go to github repo
@@ -180,15 +156,15 @@ git_setup
180156

181157
if [ -f "${GITHUB_WORKSPACE}/${ATLANTIS_FILE}" ]; then
182158
# Parse an atlantis yaml file
183-
while read -r line; do
159+
for line in $(yq e '.projects[].dir' "${GITHUB_WORKSPACE}/${ATLANTIS_FILE}"); do
184160
project_dir=${line//- /}
185161
update_doc "${project_dir}"
186-
done < <(yq e '.projects[].dir' "${GITHUB_WORKSPACE}/${ATLANTIS_FILE}")
162+
done
187163
elif [ -n "${FIND_DIR}" ] && [ "${FIND_DIR}" != "disabled" ]; then
188164
# Find all tf
189-
while read -r project_dir; do
165+
for project_dir in $(find "${FIND_DIR}" -name '*.tf' -exec dirname {} \; | uniq); do
190166
update_doc "${project_dir}"
191-
done < <(find "${FIND_DIR}" -name '*.tf' -exec dirname {} \; | uniq)
167+
done
192168
else
193169
# Split WORKING_DIR by commas
194170
for project_dir in ${WORKING_DIR//,/ }; do
@@ -200,11 +176,9 @@ if [ "${GIT_PUSH}" = "true" ]; then
200176
git_commit
201177
git push
202178
else
203-
set +e
204179
num_changed=$(git_status)
205-
set -e
206180
if [ "${FAIL_ON_DIFF}" = "true" ] && [ "${num_changed}" -ne 0 ]; then
207-
echo "::error file=entrypoint.sh,line=169::Uncommitted change(s) has been found!"
181+
echo "::error ::Uncommitted change(s) has been found!"
208182
exit 1
209183
fi
210184
echo "::set-output name=num_changed::${num_changed}"

0 commit comments

Comments
 (0)