#!/bin/bash # Development helper script for GuardDen set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Show help show_help() { cat << EOF GuardDen Development Helper Script Usage: $0 [COMMAND] Commands: setup Set up development environment test Run all tests with coverage lint Run code quality checks (ruff, mypy) format Format code with ruff security Run security scans (safety, bandit) build Build Docker images up Start development environment with Docker Compose down Stop development environment logs Show development logs clean Clean up development artifacts db Database management commands health Run health checks help Show this help message Examples: $0 setup # Set up development environment $0 test # Run tests $0 lint # Check code quality $0 up # Start development environment $0 db migrate # Run database migrations $0 health check # Run health checks EOF } # Set up development environment setup_dev() { print_status "Setting up development environment..." # Check Python version if ! command_exists python3; then print_error "Python 3 is required but not installed" exit 1 fi python_version=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')") if [[ $(echo "$python_version < 3.11" | bc -l) -eq 1 ]]; then print_warning "Python 3.11+ is recommended, you have $python_version" fi # Install dependencies print_status "Installing dependencies..." pip install -e ".[dev,monitoring]" # Set up pre-commit hooks if command_exists pre-commit; then print_status "Installing pre-commit hooks..." pre-commit install fi # Copy environment file if it doesn't exist if [[ ! -f .env ]]; then print_status "Creating .env file from template..." cp .env.example .env print_warning "Please edit .env file with your configuration" fi # Create data directories mkdir -p data logs print_success "Development environment setup complete!" print_status "Next steps:" echo " 1. Edit .env file with your Discord bot token and other settings" echo " 2. Run '$0 up' to start the development environment" echo " 3. Run '$0 test' to ensure everything is working" } # Run tests run_tests() { print_status "Running tests with coverage..." export GUARDDEN_DISCORD_TOKEN="test_token_12345678901234567890123456789012345" export GUARDDEN_DATABASE_URL="sqlite+aiosqlite:///:memory:" export GUARDDEN_AI_PROVIDER="none" export GUARDDEN_LOG_LEVEL="DEBUG" pytest --cov=src/guardden --cov-report=term-missing --cov-report=html print_success "Tests completed! Coverage report saved to htmlcov/" } # Run linting run_lint() { print_status "Running code quality checks..." echo "🔍 Running ruff (linting)..." ruff check src tests echo "🎨 Checking code formatting..." ruff format src tests --check echo "🔤 Running mypy (type checking)..." mypy src print_success "Code quality checks completed!" } # Format code format_code() { print_status "Formatting code..." echo "🎨 Formatting with ruff..." ruff format src tests echo "🔧 Fixing auto-fixable issues..." ruff check src tests --fix print_success "Code formatting completed!" } # Run security scans run_security() { print_status "Running security scans..." echo "🔒 Checking dependencies for vulnerabilities..." safety check --json --output safety-report.json || true echo "🛡️ Running security linting..." bandit -r src/ -f json -o bandit-report.json || true print_success "Security scans completed! Reports saved as *-report.json" } # Build Docker images build_docker() { print_status "Building Docker images..." echo "🐳 Building base image..." docker build -t guardden:latest . echo "Building dashboard image..." docker build -f dashboard/Dockerfile -t guardden-dashboard:latest . echo "🧠 Building image with AI dependencies..." docker build --build-arg INSTALL_AI=true -t guardden:ai . echo "🔧 Building development image..." docker build --target development -t guardden:dev . print_success "Docker images built successfully!" } # Start development environment start_dev() { print_status "Starting development environment..." if [[ ! -f .env ]]; then print_error ".env file not found. Run '$0 setup' first." exit 1 fi docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d print_success "Development environment started!" echo "📊 Services available:" echo " - Bot: Running in development mode" echo " - Dashboard: http://localhost:8080" echo " - Database: localhost:5432" echo " - Redis: localhost:6379" echo " - PgAdmin: http://localhost:5050" echo " - Redis Commander: http://localhost:8081" echo " - MailHog: http://localhost:8025" } # Stop development environment stop_dev() { print_status "Stopping development environment..." docker-compose -f docker-compose.yml -f docker-compose.dev.yml down print_success "Development environment stopped!" } # Show logs show_logs() { if [[ $# -eq 0 ]]; then docker-compose -f docker-compose.yml -f docker-compose.dev.yml logs -f else docker-compose -f docker-compose.yml -f docker-compose.dev.yml logs -f "$1" fi } # Clean up clean_up() { print_status "Cleaning up development artifacts..." # Python cache find . -type f -name "*.pyc" -delete find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true # Test artifacts rm -rf .coverage htmlcov/ .pytest_cache/ # Build artifacts rm -rf build/ dist/ *.egg-info/ # Security reports rm -f *-report.json print_success "Cleanup completed!" } # Database management manage_db() { case "${2:-help}" in "migrate"|"upgrade") print_status "Running database migrations..." python -m alembic upgrade head ;; "downgrade") print_status "Downgrading database..." python -m alembic downgrade -1 ;; "revision") if [[ -z "$3" ]]; then print_error "Please provide a revision message" echo "Usage: $0 db revision 'message'" exit 1 fi print_status "Creating new migration..." python -m alembic revision --autogenerate -m "$3" ;; "reset") print_warning "This will reset the database. Are you sure? (y/N)" read -r response if [[ "$response" =~ ^[Yy]$ ]]; then print_status "Resetting database..." python -m alembic downgrade base python -m alembic upgrade head fi ;; *) echo "Database management commands:" echo " migrate - Run pending migrations" echo " downgrade - Downgrade one migration" echo " revision - Create new migration" echo " reset - Reset database (WARNING: destructive)" ;; esac } # Health checks run_health() { case "${2:-check}" in "check") print_status "Running health checks..." python -m guardden.health --check ;; "json") python -m guardden.health --check --json ;; *) echo "Health check commands:" echo " check - Run health checks" echo " json - Run health checks with JSON output" ;; esac } # Main script logic case "${1:-help}" in "setup") setup_dev ;; "test") run_tests ;; "lint") run_lint ;; "format") format_code ;; "security") run_security ;; "build") build_docker ;; "up") start_dev ;; "down") stop_dev ;; "logs") show_logs "${@:2}" ;; "clean") clean_up ;; "db") manage_db "$@" ;; "health") run_health "$@" ;; "help"|*) show_help ;; esac