docker-pre-commit/compose-check.sh

40 lines
925 B
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
if command -v docker-compose &> /dev/null ; then
COMPOSE=docker-compose
elif command -v docker &> /dev/null && docker help compose &> /dev/null; then
COMPOSE="docker compose"
else
echo "ERROR: Neither 'docker-compose' or 'docker compose' were found"
exit 1
fi
2018-05-21 17:31:29 +00:00
check_file() {
local file=$1
env $COMPOSE --file "$file" config --quiet 2>&1 \
2018-05-21 17:31:29 +00:00
| 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
2020-01-27 22:17:55 +00:00
for file in "${all_files[@]}" ; do
2018-05-21 17:31:29 +00:00
if [[ -f "$file" ]]; then
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
}
2020-01-27 22:17:55 +00:00
if ! check_files "$@" ; then
echo "Some compose files failed"
2018-05-21 17:31:29 +00:00
fi
exit $has_error