Files
laboratori-cloud/scripts/validate-compose.sh
Luca Sacchi Ricciardi f9a3e1e342 feat(01-01): add compose validation script (validate-compose.sh)
- Validates docker-compose.yml syntax using 'docker compose config'
- Shows usage when called without arguments
- Handles missing files gracefully with clear error messages
- Supports -h/--help flag
- Color-coded output (green success, red error)
- Exit code 0 on valid config, 1 on errors

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-24 19:51:39 +01:00

95 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# Laboratori Cloud - Docker Compose Validation Script
# Part of: "Corso Soluzioni Cloud"
#
# Description: Validates docker-compose.yml syntax using 'docker compose config'
# Usage: ./scripts/validate-compose.sh [path-to-compose-file]
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print error message
print_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
# Function to print success message
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Function to print info message
print_info() {
echo "[INFO] $1"
}
# Function to print usage
print_usage() {
cat << EOF
Usage: $0 [path-to-compose-file]
Validates a docker-compose.yml file using 'docker compose config' without deploying.
Arguments:
path-to-compose-file Path to the docker-compose.yml file to validate
Options:
-h, --help Show this help message
Examples:
$0 docker-compose.yml
$0 labs/01-iam/docker-compose.yml
Exit codes:
0 - Validation successful
1 - Validation failed or file not found
EOF
}
# Check for help flag
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
print_usage
exit 0
fi
# Check if argument is provided
if [ -z "$1" ]; then
print_error "No compose file specified"
echo ""
print_usage
exit 1
fi
COMPOSE_FILE="$1"
# Check if file exists
if [ ! -f "$COMPOSE_FILE" ]; then
print_error "File not found: $COMPOSE_FILE"
exit 1
fi
print_info "Validating compose file: $COMPOSE_FILE"
# Get the directory of the compose file
COMPOSE_DIR=$(dirname "$COMPOSE_FILE")
# Try to validate using docker compose config
# We redirect stderr to stdout to capture error messages
VALIDATION_OUTPUT=$(cd "$COMPOSE_DIR" && docker compose -f "$(basename "$COMPOSE_FILE")" config 2>&1)
VALIDATION_EXIT=$?
if [ $VALIDATION_EXIT -eq 0 ]; then
print_success "Compose file validation passed"
print_info "YAML syntax is valid and all directives are recognized"
exit 0
else
print_error "Compose file validation failed"
echo ""
echo "Docker Compose output:"
echo "$VALIDATION_OUTPUT"
exit 1
fi