14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
- set -e
17
+ set -o errexit
18
+ set -o pipefail
19
+ set -o errtrace
18
20
19
21
# Ensure all variables are present
20
22
WORKING_DIR=" ${1} "
@@ -71,9 +73,9 @@ git_add() {
71
73
file=" $1 "
72
74
git add " ${file} "
73
75
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"
75
77
else
76
- echo " ::debug file=entrypoint.sh,line=48 No change in ${file} detected"
78
+ echo " ::debug No change in ${file} detected"
77
79
fi
78
80
}
79
81
@@ -82,95 +84,69 @@ git_status() {
82
84
}
83
85
84
86
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"
91
89
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} "
99
90
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} "
100
97
}
101
98
102
99
update_doc () {
103
100
local working_dir
104
- local generated
105
- local success
106
101
107
102
working_dir=" $1 "
108
- echo " ::debug file=entrypoint.sh,line=66 working_dir=${working_dir} "
103
+ echo " ::debug working_dir=${working_dir} "
109
104
110
- set +e
105
+ local config_file
111
106
112
- # shellcheck disable=SC2086
113
107
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
116
108
if [ -f " ${CONFIG_FILE} " ]; then
117
109
config_file=" ${CONFIG_FILE} "
118
110
else
119
111
config_file=" ${working_dir} /${CONFIG_FILE} "
120
112
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
130
113
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} "
135
116
fi
136
117
137
- generated= $( cat /tmp/tf_generated )
138
- rm -f /tmp/tf_generated
118
+ local output_mode
119
+ local output_file
139
120
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} "
144
124
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
149
128
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
155
130
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=$?
159
141
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
165
145
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
170
147
git_add " ${working_dir} /${OUTPUT_FILE} "
171
- rm -f /tmp/tf_doc.md
172
- ;;
173
- esac
148
+ fi
149
+
174
150
}
175
151
176
152
# go to github repo
@@ -180,15 +156,15 @@ git_setup
180
156
181
157
if [ -f " ${GITHUB_WORKSPACE} /${ATLANTIS_FILE} " ]; then
182
158
# Parse an atlantis yaml file
183
- while read -r line ; do
159
+ for line in $( yq e ' .projects[].dir ' " ${GITHUB_WORKSPACE} / ${ATLANTIS_FILE} " ) ; do
184
160
project_dir=${line// - / }
185
161
update_doc " ${project_dir} "
186
- done < <( yq e ' .projects[].dir ' " ${GITHUB_WORKSPACE} / ${ATLANTIS_FILE} " )
162
+ done
187
163
elif [ -n " ${FIND_DIR} " ] && [ " ${FIND_DIR} " != " disabled" ]; then
188
164
# Find all tf
189
- while read -r project_dir ; do
165
+ for project_dir in $( find " ${FIND_DIR} " -name ' *.tf ' -exec dirname {} \; | uniq ) ; do
190
166
update_doc " ${project_dir} "
191
- done < <( find " ${FIND_DIR} " -name ' *.tf ' -exec dirname {} \; | uniq )
167
+ done
192
168
else
193
169
# Split WORKING_DIR by commas
194
170
for project_dir in ${WORKING_DIR// ,/ } ; do
@@ -200,11 +176,9 @@ if [ "${GIT_PUSH}" = "true" ]; then
200
176
git_commit
201
177
git push
202
178
else
203
- set +e
204
179
num_changed=$( git_status)
205
- set -e
206
180
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!"
208
182
exit 1
209
183
fi
210
184
echo " ::set-output name=num_changed::${num_changed} "
0 commit comments