#!/bin/bash # Color definitions RESET='\033[0m' BOLD='\033[1m' RED='\033[91m' GREEN='\033[92m' YELLOW='\033[93m' BLUE='\033[94m' MAGENTA='\033[95m' CYAN='\033[96m' # Directory containing YAML files MATCH_DIR="match" # Check if directory exists if [ ! -d "$MATCH_DIR" ]; then echo -e "${RED}${BOLD}Error:${RESET} ${RED}Directory '$MATCH_DIR' not found!${RESET}" exit 1 fi # Find all YAML files YAML_FILES=$(find "$MATCH_DIR" -name "*.yml" -o -name "*.yaml" | sort) if [ -z "$YAML_FILES" ]; then echo -e "${YELLOW}${BOLD}Warning:${RESET} ${YELLOW}No YAML files found in $MATCH_DIR directory!${RESET}" exit 0 fi # Function to check if yamllint is available check_yamllint() { if command -v yamllint &> /dev/null; then echo "yamllint" return 0 fi if command -v pipx &> /dev/null && pipx list | grep -q yamllint; then echo "pipx run yamllint" return 0 fi if python3 -m yamllint --version &> /dev/null 2>&1; then echo "python3 -m yamllint" return 0 fi return 1 } # Function to use Python's yaml module for basic validation validate_with_python() { local file="$1" python3 << EOF import yaml import sys try: with open("$file", 'r') as f: yaml.safe_load(f) sys.exit(0) except yaml.YAMLError as e: print(f"$file: {e}", file=sys.stderr) sys.exit(1) except Exception as e: print(f"$file: Error reading file - {e}", file=sys.stderr) sys.exit(1) EOF } # Try to find yamllint YAMLLINT_CMD=$(check_yamllint) if [ -n "$YAMLLINT_CMD" ]; then echo -e "${CYAN}${BOLD}Using yamllint for YAML linting...${RESET}" echo "" # Run yamllint on all files ERRORS=0 for file in $YAML_FILES; do echo -e "${BLUE}Linting:${RESET} ${BOLD}$(basename "$file")${RESET}" if ! $YAMLLINT_CMD "$file"; then ERRORS=$((ERRORS + 1)) fi done if [ $ERRORS -eq 0 ]; then echo "" echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All YAML files passed linting!${RESET}" exit 0 else echo "" echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Found linting errors in $ERRORS file(s)${RESET}" exit 1 fi else echo -e "${YELLOW}${BOLD}Warning:${RESET} ${YELLOW}yamllint not found. Falling back to basic YAML syntax validation...${RESET}" echo "" echo -e "${CYAN}${BOLD}Tip:${RESET} ${CYAN}Install yamllint for better linting:${RESET}" echo -e " ${BLUE}•${RESET} ${MAGENTA}pipx install yamllint${RESET} (recommended - avoids system package conflicts)" echo -e " ${BLUE}•${RESET} ${MAGENTA}pip install --user yamllint${RESET}" echo "" # Fall back to Python yaml validation ERRORS=0 for file in $YAML_FILES; do echo -e "${BLUE}Validating:${RESET} ${BOLD}$(basename "$file")${RESET}" if ! validate_with_python "$file" 2>&1; then ERRORS=$((ERRORS + 1)) else echo -e " ${GREEN}✓${RESET} Valid YAML syntax" fi done if [ $ERRORS -eq 0 ]; then echo "" echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All YAML files have valid syntax!${RESET}" echo -e "${YELLOW}${BOLD}Note:${RESET} ${YELLOW}Install yamllint for style and best-practice checks${RESET}" exit 0 else echo "" echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Found syntax errors in $ERRORS file(s)${RESET}" exit 1 fi fi