Skip to content

Commit cc20e2d

Browse files
Copilotjosecelano
andcommitted
feat: [#3] add CLI logging initialization with configurable options
Co-authored-by: josecelano <[email protected]>
1 parent d37bd42 commit cc20e2d

File tree

2 files changed

+104
-19
lines changed

2 files changed

+104
-19
lines changed

src/app.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

src/main.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
//! Main binary entry point for Torrust Tracker Deployer.
22
//!
3-
//! This binary provides a simple information display about the deployment infrastructure.
3+
//! This binary provides the main CLI interface for the deployment infrastructure.
4+
//! All application logic is contained in the `app` module.
5+
6+
mod app;
47

58
fn main() {
6-
println!("🏗️ Torrust Tracker Deployer");
7-
println!("=========================");
8-
println!();
9-
println!("This repository provides automated deployment infrastructure for Torrust tracker projects.");
10-
println!("The infrastructure includes VM provisioning with OpenTofu and configuration");
11-
println!("management with Ansible playbooks.");
12-
println!();
13-
println!("📋 Getting Started:");
14-
println!(" Please follow the instructions in the README.md file to:");
15-
println!(" 1. Set up the required dependencies (OpenTofu, Ansible, LXD)");
16-
println!(" 2. Provision the deployment infrastructure");
17-
println!(" 3. Deploy and configure the services");
18-
println!();
19-
println!("🧪 Running E2E Tests:");
20-
println!(" Use the e2e tests binaries to run end-to-end tests:");
21-
println!(" cargo e2e-provision && cargo e2e-config");
22-
println!();
23-
println!("📖 For detailed instructions, see: README.md");
9+
app::run();
2410
}

0 commit comments

Comments
 (0)