Skip to content

Commit

Permalink
DRY out hook functionality
Browse files Browse the repository at this point in the history
Create a utility library to handle argument processing since it is used
by both hooks. Also put the logic for getting unique directory paths
there since it makes sense to put that kind of functionality in a
utility library.
  • Loading branch information
mcdonnnj committed Sep 16, 2024
1 parent 1fd00ef commit 6b76466
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 49 deletions.
28 changes: 8 additions & 20 deletions hooks/packer_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,18 @@ if [ -z "$(command -v packer)" ]; then
exit 1
fi

args=()
files=()
# The version of readlink on macOS does not support long options
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
readonly SCRIPT_DIR
# shellcheck source=lib/util.sh
source "$SCRIPT_DIR/../lib/util.sh"

while (("$#")); do
case "$1" in
-*)
if [ -f "$1" ]; then
files+=("$1")
else
args+=("$1")
fi
shift
;;
*)
files+=("$1")
shift
;;
esac
done
util::parse_cmdline "$@"

error=0

for file in "${files[@]}"; do
if ! packer fmt "${args[@]}" -- "$file"; then
for file in "${FILES[@]}"; do
if ! packer fmt "${ARGS[@]}" -- "$file"; then
error=1
echo
echo "Failed path: $file"
Expand Down
38 changes: 9 additions & 29 deletions hooks/packer_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,20 @@ if [ -z "$(command -v packer)" ]; then
exit 1
fi

args=()
files=()

while (("$#")); do
case "$1" in
-*)
if [ -f "$1" ]; then
files+=("$1")
else
args+=("$1")
fi
shift
;;
*)
files+=("$1")
shift
;;
esac
done
# The version of readlink on macOS does not support long options
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
readonly SCRIPT_DIR
# shellcheck source=lib/util.sh
source "$SCRIPT_DIR/../lib/util.sh"

paths=()
index=0
for file in "${files[@]}"; do
paths[index]=$(dirname -- "$file")
((++index))
done
util::parse_cmdline "$@"

unique_paths=()
while IFS='' read -r line; do unique_paths+=("$line"); done < <(printf '%s\n' "${paths[@]}" | sort --unique)
util::get_unique_directory_paths "${FILES[@]}"

error=0

for path in "${unique_paths[@]}"; do
if ! packer validate "${args[@]}" -- "$path"; then
for path in "${UNIQUE_PATHS[@]}"; do
if ! packer validate "${ARGS[@]}" -- "$path"; then
error=1
echo
echo "Failed path: $path"
Expand Down
55 changes: 55 additions & 0 deletions lib/util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

set -o nounset
set -o errexit
set -o pipefail

#######################################
# Process the command line and separate arguments from files
# Globals:
# ARGS
# FILES
#######################################
function util::parse_cmdline() {
# Global variable arrays
ARGS=()
FILES=()

while (("$#")); do
case "$1" in
-*)
if [ -f "$1" ]; then
FILES+=("$1")
else
ARGS+=("$1")
fi
shift
;;
*)
FILES+=("$1")
shift
;;
esac
done
}

#######################################
# Create a list of unique directory paths from a list of file paths
# Globals:
# UNIQUE_PATHS
#######################################
function util::get_unique_directory_paths() {
# Global variable arrays
UNIQUE_PATHS=()

local -a paths

index=0
for file in "$@"; do
paths[index]=$(dirname -- "$file")
((++index))
done

UNIQUE_PATHS=()
while IFS='' read -r line; do UNIQUE_PATHS+=("$line"); done < <(printf '%s\n' "${paths[@]}" | sort --unique)
}

0 comments on commit 6b76466

Please sign in to comment.