Skip to content

Commit 0d4ffdb

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

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bash
22

3+
set -x
34
set -o nounset
45
set -o errexit
56
set -o pipefail
@@ -22,10 +23,18 @@ util::get_unique_directory_paths "${FILES[@]}"
2223
error=0
2324

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

27-
packer init . > /dev/null
28-
if ! packer validate "${ARGS[@]}" .; then
37+
if [[ $lerror -ne 0 ]]; then
2938
error=1
3039
echo
3140
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)