Skip to content

Commit

Permalink
Use directories instead of individual files
Browse files Browse the repository at this point in the history
This change updates the `packer_validate` hook to run against
directories instead of against each file. This will correctly allow
configurations to be split across multiple files but be treated as a
single configuration when the hook is run.
  • Loading branch information
mcdonnnj committed Sep 10, 2024
1 parent c49bdd1 commit 0303240
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
language: script
files: \.pkr\.hcl$
pass_filenames: true
require_serial: true

- id: packer_fmt
name: Packer Format
Expand Down
16 changes: 13 additions & 3 deletions hooks/packer_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ while (("$#")); do
esac
done

paths=()
index=0
for file in "${files[@]}"; do
paths[index]=$(dirname "$file")
((++index))
done

unique_paths=()
while IFS='' read -r line; do unique_paths+=("$line"); done < <(printf '%s\n' "${paths[@]}" | sort --unique)

error=0

for file in "${files[@]}"; do
if ! packer validate "${args[@]}" "$file"; then
for path in "${unique_paths[@]}"; do
if ! packer validate "${args[@]}" "$path"; then
error=1
echo
echo "Failed path: $file"
echo "Failed path: $path"
echo "================================"
fi
done
Expand Down

0 comments on commit 0303240

Please sign in to comment.