Skip to content

Commit c862c4b

Browse files
committed
fix: cleanup internal argument handling
ARGS and FILES are global arrays - no need to pass them as function arguments
1 parent 45e16de commit c862c4b

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

terraform_docs.sh

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -eo pipefail
44
main() {
55
initialize_
66
parse_cmdline_ "$@"
7-
terraform_docs_ "${ARGS[*]}" "${FILES[@]}"
7+
terraform_docs_
88
}
99

1010
initialize_() {
@@ -47,10 +47,6 @@ parse_cmdline_() {
4747
}
4848

4949
terraform_docs_() {
50-
local -r args="$1"
51-
shift
52-
local -a -r files=("$@")
53-
5450
local hack_terraform_docs
5551
hack_terraform_docs=$(terraform version | head -1 | grep -c 0.12) || true
5652

@@ -64,7 +60,7 @@ terraform_docs_() {
6460

6561
if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred)
6662

67-
terraform_docs "0" "$args" "${files[@]}"
63+
terraform_docs "0"
6864

6965
elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used
7066

@@ -76,28 +72,25 @@ terraform_docs_() {
7672
local tmp_file_awk
7773
tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
7874
terraform_docs_awk "$tmp_file_awk"
79-
terraform_docs "$tmp_file_awk" "$args" "${files[@]}"
75+
terraform_docs "$tmp_file_awk"
8076
rm -f "$tmp_file_awk"
8177

8278
else # Using terraform 0.11 and no awk script is needed for that
8379

84-
terraform_docs "0" "$args" "${files[@]}"
80+
terraform_docs "0"
8581

8682
fi
8783
}
8884

8985
terraform_docs() {
90-
local -r terraform_docs_awk_file="$1"
91-
local -r args="$2"
92-
shift 2
93-
local -a -r files=("$@")
86+
local -r terraform_docs_awk_file="$1" ; shift
9487

9588
declare -a paths
9689
declare -a tfvars_files
9790

9891
local index=0
9992
local file_with_path
100-
for file_with_path in "${files[@]}"; do
93+
for file_with_path in "${FILES[@]}"; do
10194
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
10295

10396
paths[index]=$(dirname "$file_with_path")
@@ -125,18 +118,18 @@ terraform_docs() {
125118

126119
if [[ "$terraform_docs_awk_file" == "0" ]]; then
127120
# shellcheck disable=SC2086
128-
terraform-docs md $args ./ > "$tmp_file"
121+
terraform-docs md "${ARGS[*]}" ./ > "$tmp_file"
129122
else
130123
# Can't append extension for mktemp, so renaming instead
131124
local tmp_file_docs
132-
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
133-
mv "$tmp_file_docs" "$tmp_file_docs.tf"
134125
local tmp_file_docs_tf
126+
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
135127
tmp_file_docs_tf="$tmp_file_docs.tf"
128+
mv "$tmp_file_docs" "$tmp_file_docs_tf"
136129

137130
awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
138131
# shellcheck disable=SC2086
139-
terraform-docs md $args "$tmp_file_docs_tf" > "$tmp_file"
132+
terraform-docs md "${ARGS[*]}" "$tmp_file_docs_tf" > "$tmp_file"
140133
rm -f "$tmp_file_docs_tf"
141134
fi
142135

terraform_tfsec.sh

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@ set -eo pipefail
44
main() {
55
initialize_
66
parse_cmdline_ "$@"
7-
8-
# propagate $FILES to custom function
9-
tfsec_ "$ARGS" "$FILES"
7+
tfsec_
108
}
119

1210
tfsec_() {
1311
# consume modified files passed from pre-commit so that
1412
# tfsec runs against only those relevant directories
15-
for file_with_path in $FILES; do
13+
for file_with_path in "${FILES[@]}"; do
1614
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
1715
paths[index]=$(dirname "$file_with_path")
18-
19-
let "index+=1"
16+
(( index+=1 ))
2017
done
2118

2219
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
2320
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
2421
pushd "$path_uniq" > /dev/null
25-
tfsec $ARGS
22+
tfsec "${ARGS[@]}"
2623
popd > /dev/null
2724
done
2825
}

0 commit comments

Comments
 (0)