Skip to content

Commit

Permalink
feat: allow disabling pushd with --no-cd
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecat committed Dec 22, 2024
1 parent b8e2684 commit 28c36aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
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

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

0 comments on commit 28c36aa

Please sign in to comment.