#!/bin/bash # Enhanced version of the original run.sh script with explicit container management set -euo pipefail # Project metadata readonly PROJECT_NAME="KNEL Football" readonly VERSION="1.0.0" # Configuration readonly DOCKER_IMAGE="${DOCKER_IMAGE:-knel-football-dev:latest}" # Using required knel-football-dev image readonly CONTAINER_PREFIX="knel-football" readonly PROXY_ENABLED="${PROXY_ENABLED:-true}" readonly PROXY_URL="${PROXY_URL:-http://10.0.0.1:3128}" # Source utility functions source "$(dirname "$0")/lib/docker.sh" # Logging function log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" } # Usage information usage() { cat <&2 } log_debug() { if [ "$VERBOSE" = true ]; then log "DEBUG: $*" fi } # Container management run_with_container() { local cmd="$1" local container_name="${CONTAINER_PREFIX}-${cmd}" shift log_info "Starting container: $container_name" log_debug "Command: $*" # Build environment arguments local env_args=() for env_var in "${ENV_VARS[@]}"; do env_args+=("-e" "$env_var") done if [ "$USE_PROXY" = true ]; then env_args+=("-e" "http_proxy=$PROXY_URL") env_args+=("-e" "https_proxy=$PROXY_URL") fi # Run container with explicit name and environment docker run --name "$container_name" \ --env-file <(grep -v '^#' "$(dirname "$0")/.env" 2>/dev/null || true) \ "${env_args[@]}" \ -v "$(pwd)":/workspace:ro \ -v "$(pwd)/tmp":/build \ -v "$(pwd)/output":/output \ -e TZ="UTC" \ -e DEBIAN_FRONTEND="noninteractive" \ -e LC_ALL="C" \ --rm \ "$DOCKER_IMAGE" \ "$@" } # Main command handlers cmd_build() { log_info "Building Docker image: $DOCKER_IMAGE" local build_args=() if [ "$NO_CACHE" = true ]; then build_args+=("--no-cache") fi if [ "$USE_PROXY" = true ]; then build_args+=("--build-arg" "http_proxy=$PROXY_URL") build_args+=("--build-arg" "https_proxy=$PROXY_URL") fi docker build "${build_args[@]}" -t "$DOCKER_IMAGE" "$(dirname "$0")" } cmd_lint() { log_info "Running lint checks" run_with_container "lint" make lint } cmd_test() { log_info "Running all tests" run_with_container "test" make test } cmd_test_unit() { log_info "Running unit tests" run_with_container "test-unit" make test-unit } cmd_test_integration() { log_info "Running integration tests" run_with_container "test-integration" make test-integration } cmd_test_functional() { log_info "Running functional tests" run_with_container "test-functional" make test-functional } cmd_shell() { log_info "Starting interactive shell" run_with_container "shell" /bin/bash } cmd_clean() { log_info "Cleaning build artifacts" rm -rf "$(dirname "$0")/tmp" mkdir -p "$(dirname "$0")/tmp" log_info "Cleanup completed" } cmd_iso() { log_info "Building ISO image" run_with_container "iso" make iso } cmd_secure() { log_info "Generating security configuration" run_with_container "secure" make secure } cmd_deploy() { log_info "Preparing deployment package" run_with_container "deploy" make deploy } # Execute command case "$COMMAND" in build) cmd_build ;; lint) cmd_lint ;; test) cmd_test ;; test:unit) cmd_test_unit ;; test:integration) cmd_test_integration ;; test:functional) cmd_test_functional ;; shell) cmd_shell ;; clean) cmd_clean ;; iso) cmd_iso ;; secure) cmd_secure ;; deploy) cmd_deploy ;; help) usage ;; *) log_error "Unknown command: $COMMAND" usage exit 1 ;; esac