rustchain/cli/
mod.rs

1pub mod commands;
2pub mod handlers;
3pub mod interactive;
4pub mod help_examples;
5// Disabled for now due to compilation issues
6// pub mod commands_pretty;
7// pub mod handlers_pretty;
8// pub mod pretty;
9
10use clap::Parser;
11use commands::Cli;
12use handlers::handle_command;
13
14/// Main CLI entry point
15pub async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
16    let args = Cli::parse();
17
18    // Initialize logging
19    init_logging()?;
20
21    // Handle the command with enhanced error formatting
22    if let Err(_e) = handle_command(args.command).await {
23        // Error is already formatted by handle_command, just return
24        std::process::exit(1);
25    }
26
27    Ok(())
28}
29
30/// Initialize logging system
31fn init_logging() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
32    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
33
34    // Only initialize if not already initialized
35    if std::env::var("RUST_LOG").is_err() {
36        std::env::set_var("RUST_LOG", "info");
37    }
38
39    // Try to initialize, but don't fail if already initialized
40    let _result = tracing_subscriber::registry()
41        .with(tracing_subscriber::EnvFilter::from_default_env())
42        .with(tracing_subscriber::fmt::layer())
43        .try_init();
44
45    Ok(())
46}
47
48/// Graceful error handling
49pub fn handle_error(error: &dyn std::error::Error) {
50    eprintln!("Fatal error: {}", error);
51    tracing::error!("Application error: {}", error);
52}