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 13, 2024
1 parent f951a38 commit 0d4ffdb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 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
15 changes: 12 additions & 3 deletions hooks/packer_validate.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

set -x
set -o nounset
set -o errexit
set -o pipefail
Expand All @@ -22,10 +23,18 @@ util::get_unique_directory_paths "${FILES[@]}"
error=0

for path in "${UNIQUE_PATHS[@]}"; do
pushd "$path" > /dev/null
if [[ $NO_CD -eq 1 ]]; then
packer init -- "$path" > /dev/null
packer validate "${ARGS[@]}" -- "$path"
lerror=$?
else
pushd "$path" > /dev/null
packer init . > /dev/null
packer validate "${ARGS[@]}" .
lerror=$?
fi

packer init . > /dev/null
if ! packer validate "${ARGS[@]}" .; then
if [[ $lerror -ne 0 ]]; then
error=1
echo
echo "Failed path: $path"
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 0d4ffdb

Please sign in to comment.