Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ repos:
hooks:
- id: packer_fmt
- id: packer_validate
args: [--no-cd] # Optionally disable changing working directory
```

## Contributing ##
Expand Down
12 changes: 8 additions & 4 deletions hooks/packer_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ set -o pipefail
function packer_validate() {
local exit_code=0

packer init . > /dev/null
packer init "$1" > /dev/null

# Allow us to get output if the validation fails
set +o errexit
validate_output=$(packer validate "${ARGS[@]}" . 2>&1)
validate_output=$(packer validate "${ARGS[@]}" "$1" 2>&1)
exit_code=$?
set -o errexit

Expand Down Expand Up @@ -42,8 +42,12 @@ pids=()
for path in "${UNIQUE_PATHS[@]}"; do
# Check each path in parallel
{
pushd "$path" > /dev/null
packer_validate
if [[ $NO_CD -eq 1 ]]; then
packer_validate "$path"
else
pushd "$path" > /dev/null
packer_validate .
fi
} &
pids+=("$!")
done
Expand Down
6 changes: 6 additions & 0 deletions lib/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ set -o pipefail
# Globals:
# ARGS
# FILES
# NO_CD
#######################################
function util::parse_cmdline() {
# Global variable arrays
ARGS=()
FILES=()
export NO_CD=0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export is needed to silence shellcheck SC2034.
It works without it, but shellcheck complains:

shellcheck...............................................................Failed
- hook id: shellcheck
- exit code: 1

In lib/util.sh line 23:
        NO_CD=1
        ^---^ SC2034 (warning): NO_CD appears unused. Verify use (or export if used externally).


while (("$#")); do
case "$1" in
--no-cd)
NO_CD=1
shift
;;
-*)
if [ -f "$1" ]; then
FILES+=("$1")
Expand Down
Loading