|
| 1 | +//! Main application module for the Torrust Tracker Deployer CLI |
| 2 | +//! |
| 3 | +//! This module contains the CLI structure and main application logic. |
| 4 | +//! It initializes logging and handles the application lifecycle. |
| 5 | +
|
| 6 | +use clap::Parser; |
| 7 | +use std::path::PathBuf; |
| 8 | +use tracing::info; |
| 9 | + |
| 10 | +use torrust_tracker_deployer_lib::logging::{LogFormat, LogOutput, LoggingBuilder}; |
| 11 | + |
| 12 | +/// Command-line interface for Torrust Tracker Deployer |
| 13 | +#[derive(Parser)] |
| 14 | +#[command(name = "torrust-tracker-deployer")] |
| 15 | +#[command(about = "Automated deployment infrastructure for Torrust Tracker")] |
| 16 | +#[command(version)] |
| 17 | +pub struct Cli { |
| 18 | + /// Logging format (default: compact) |
| 19 | + /// |
| 20 | + /// - pretty: Pretty-printed output for development |
| 21 | + /// - json: JSON output for production environments |
| 22 | + /// - compact: Compact output for minimal verbosity |
| 23 | + #[arg(long, value_enum, default_value = "compact", global = true)] |
| 24 | + pub log_format: LogFormat, |
| 25 | + |
| 26 | + /// Log output mode (default: file-only for production) |
| 27 | + /// |
| 28 | + /// - file-only: Write logs to file only (production mode) |
| 29 | + /// - file-and-stderr: Write logs to both file and stderr (development/testing mode) |
| 30 | + #[arg(long, value_enum, default_value = "file-only", global = true)] |
| 31 | + pub log_output: LogOutput, |
| 32 | + |
| 33 | + /// Log directory (default: ./data/logs) |
| 34 | + /// |
| 35 | + /// Directory where log files will be written. The log file will be |
| 36 | + /// named 'log.txt' inside this directory. Parent directories will be |
| 37 | + /// created automatically if they don't exist. |
| 38 | + #[arg(long, default_value = "./data/logs", global = true)] |
| 39 | + pub log_dir: PathBuf, |
| 40 | +} |
| 41 | + |
| 42 | +/// Main application entry point |
| 43 | +/// |
| 44 | +/// This function initializes logging, displays information to the user, |
| 45 | +/// and prepares the application for future command processing. |
| 46 | +/// |
| 47 | +/// # Panics |
| 48 | +/// |
| 49 | +/// This function will panic if: |
| 50 | +/// - Log directory cannot be created (filesystem permissions issue) |
| 51 | +/// - Logging initialization fails (usually means it was already initialized) |
| 52 | +/// |
| 53 | +/// Both panics are intentional as logging is critical for observability. |
| 54 | +pub fn run() { |
| 55 | + let cli = Cli::parse(); |
| 56 | + |
| 57 | + // Clone values for logging before moving them |
| 58 | + let log_format = cli.log_format.clone(); |
| 59 | + let log_output = cli.log_output; |
| 60 | + let log_dir = cli.log_dir.clone(); |
| 61 | + |
| 62 | + // Initialize logging FIRST before any other logic |
| 63 | + LoggingBuilder::new(&cli.log_dir) |
| 64 | + .with_format(cli.log_format) |
| 65 | + .with_output(cli.log_output) |
| 66 | + .init(); |
| 67 | + |
| 68 | + // Log startup event with configuration details |
| 69 | + info!( |
| 70 | + app = "torrust-tracker-deployer", |
| 71 | + version = env!("CARGO_PKG_VERSION"), |
| 72 | + log_dir = %log_dir.display(), |
| 73 | + log_format = ?log_format, |
| 74 | + log_output = ?log_output, |
| 75 | + "Application started" |
| 76 | + ); |
| 77 | + |
| 78 | + // Display info to user (keep existing behavior for now) |
| 79 | + println!("🏗️ Torrust Tracker Deployer"); |
| 80 | + println!("========================="); |
| 81 | + println!(); |
| 82 | + println!("This repository provides automated deployment infrastructure for Torrust tracker projects."); |
| 83 | + println!("The infrastructure includes VM provisioning with OpenTofu and configuration"); |
| 84 | + println!("management with Ansible playbooks."); |
| 85 | + println!(); |
| 86 | + println!("📋 Getting Started:"); |
| 87 | + println!(" Please follow the instructions in the README.md file to:"); |
| 88 | + println!(" 1. Set up the required dependencies (OpenTofu, Ansible, LXD)"); |
| 89 | + println!(" 2. Provision the deployment infrastructure"); |
| 90 | + println!(" 3. Deploy and configure the services"); |
| 91 | + println!(); |
| 92 | + println!("🧪 Running E2E Tests:"); |
| 93 | + println!(" Use the e2e tests binaries to run end-to-end tests:"); |
| 94 | + println!(" cargo e2e-provision && cargo e2e-config"); |
| 95 | + println!(); |
| 96 | + println!("📖 For detailed instructions, see: README.md"); |
| 97 | + |
| 98 | + info!("Application finished"); |
| 99 | +} |
0 commit comments