Skip to content

Commit d45af30

Browse files
committed
feat: allow disabling pushd with --no-cd
1 parent f951a38 commit d45af30

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ repos:
3838
hooks:
3939
- id: packer_fmt
4040
- id: packer_validate
41+
args: [--no-cd] # Optionally disable changing working directory
4142
```
4243
4344
## Contributing ##

hooks/packer_validate.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ util::get_unique_directory_paths "${FILES[@]}"
2222
error=0
2323

2424
for path in "${UNIQUE_PATHS[@]}"; do
25-
pushd "$path" > /dev/null
25+
if [[ $NO_CD -eq 1 ]]; then
26+
packer init -- "$path" > /dev/null
27+
packer validate "${ARGS[@]}" -- "$path"
28+
lerror=$?
29+
else
30+
pushd "$path" > /dev/null
31+
packer init . > /dev/null
32+
packer validate "${ARGS[@]}" .
33+
lerror=$?
34+
fi
2635

27-
packer init . > /dev/null
28-
if ! packer validate "${ARGS[@]}" .; then
36+
if [[ $lerror -ne 0 ]]; then
2937
error=1
3038
echo
3139
echo "Failed path: $path"

lib/util.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ set -o pipefail
99
# Globals:
1010
# ARGS
1111
# FILES
12+
# NO_CD
1213
#######################################
1314
function util::parse_cmdline() {
1415
# Global variable arrays
1516
ARGS=()
1617
FILES=()
18+
export NO_CD=0
1719

1820
while (("$#")); do
1921
case "$1" in
22+
--no-cd)
23+
NO_CD=1
24+
shift
25+
;;
2026
-*)
2127
if [ -f "$1" ]; then
2228
FILES+=("$1")

0 commit comments

Comments
 (0)