diff --git a/README.md b/README.md index e6863dd..c275716 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ repos: hooks: - id: packer_fmt - id: packer_validate + args: [--no-cd] # Optionally disable changing working directory ``` ## Contributing ## diff --git a/hooks/packer_validate.sh b/hooks/packer_validate.sh index e64dba9..17b7a0f 100755 --- a/hooks/packer_validate.sh +++ b/hooks/packer_validate.sh @@ -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 @@ -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 diff --git a/lib/util.sh b/lib/util.sh index d0ad9aa..015180a 100755 --- a/lib/util.sh +++ b/lib/util.sh @@ -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")