docker-pre-commit/compose-check.sh

52 lines
1.3 KiB
Bash
Raw Normal View History

#! /usr/bin/env bash
2018-05-21 17:31:29 +00:00
# Verifies that files passed in are valid for docker-compose
set -e
2024-05-12 16:50:33 +00:00
# Check if docker or podman commands are available
if [[ -z "${CONTAINER_ENGINE}" ]]; then
if command -v docker &>/dev/null; then
CONTAINER_ENGINE=docker
elif command -v podman &>/dev/null; then
CONTAINER_ENGINE=podman
else
echo "ERROR: Neither 'docker' or 'podman' were found"
exit 1
fi
fi
if command -v "${CONTAINER_ENGINE}" &>/dev/null && ${CONTAINER_ENGINE} help compose &> /dev/null; then
COMPOSE="${CONTAINER_ENGINE} compose"
elif command -v "${CONTAINER_ENGINE}-compose" &> /dev/null; then
COMPOSE="${CONTAINER_ENGINE}-compose"
else
2024-05-12 16:50:33 +00:00
echo "ERROR: Neither '${CONTAINER_ENGINE}-compose' or '${CONTAINER_ENGINE} compose' were found"
exit 1
fi
2018-05-21 17:31:29 +00:00
check_file() {
local file=$1
2024-05-12 16:50:33 +00:00
env $COMPOSE --file "$file" config --quiet 2>&1 |
sed "/variable is not set. Defaulting/d"
2020-01-27 22:17:55 +00:00
return "${PIPESTATUS[0]}"
2018-05-21 17:31:29 +00:00
}
check_files() {
2020-01-27 22:17:55 +00:00
local all_files=( "$@" )
2018-05-21 17:31:29 +00:00
has_error=0
2024-05-12 16:50:33 +00:00
for file in "${all_files[@]}"; do
2018-05-21 17:31:29 +00:00
if [[ -f "$file" ]]; then
2024-05-12 16:50:33 +00:00
if ! check_file "$file"; then
2020-01-27 22:17:55 +00:00
echo "ERROR: $file"
2018-05-21 17:31:29 +00:00
has_error=1
fi
fi
done
return $has_error
}
2024-05-12 16:50:33 +00:00
if ! check_files "$@"; then
echo "Some compose files failed"
2018-05-21 17:31:29 +00:00
fi
exit $has_error