aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart/src/main.rs
blob: 8201d878debaad1d9e689240bcd669fcd052b124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use clap::Parser;
use mozart::commands;
use mozart_core::exit_code;
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

fn init_tracing(profile: bool, verbose: u8, quiet: bool) {
    // MOZART_LOG environment variable takes highest priority.
    if let Ok(env_filter) = EnvFilter::try_from_env("MOZART_LOG") {
        tracing_subscriber::registry()
            .with(fmt::layer().with_writer(std::io::stderr))
            .with(env_filter)
            .init();
        return;
    }

    if profile {
        let filter = match verbose {
            0 => "mozart=info",
            1 | 2 => "mozart=debug",
            _ => "mozart=trace",
        };
        tracing_subscriber::registry()
            .with(
                fmt::layer()
                    .with_writer(std::io::stderr)
                    .with_timer(fmt::time::uptime())
                    .with_span_events(fmt::format::FmtSpan::CLOSE),
            )
            .with(EnvFilter::new(filter))
            .init();
    } else if verbose >= 3 && !quiet {
        tracing_subscriber::registry()
            .with(fmt::layer().with_writer(std::io::stderr).with_target(false))
            .with(EnvFilter::new("mozart=debug"))
            .init();
    }
    // Otherwise: no subscriber installed → tracing macros are effectively zero-cost no-ops.
}

#[tokio::main]
async fn main() {
    let cli = commands::Cli::parse();

    if cli.version {
        let build_date = option_env!("MOZART_BUILD_DATE").unwrap_or("source");
        // Line 1 (stdout): getLongVersion() equivalent
        println!(
            "{}",
            mozart_core::console_format!(
                "<info>Mozart</info> version <comment>{}</comment> {}",
                env!("CARGO_PKG_VERSION"),
                build_date
            )
        );
        // Line 2 (stderr): PHP version + binary path (matches Composer's output)
        let (php_version, php_binary) = mozart_core::platform::detect_php_version_and_binary()
            .unwrap_or_else(|| ("not found".into(), "php".into()));
        eprintln!(
            "{}",
            mozart_core::console_format!(
                "<info>PHP</info> version <comment>{}</comment> ({})",
                php_version,
                php_binary
            )
        );
        // Line 3 (stderr): diagnose hint
        eprintln!("Run the \"diagnose\" command to get more detailed diagnostics output.");
        return;
    }

    let Some(ref _cmd) = cli.command else {
        use clap::CommandFactory;
        commands::Cli::command().print_help().ok();
        println!();
        return;
    };

    init_tracing(cli.profile, cli.verbose, cli.quiet);
    match commands::execute(&cli).await {
        Ok(()) => {}
        Err(e) => {
            // Check if this is a structured MozartError with a specific exit code.
            if let Some(mozart_err) = e.downcast_ref::<exit_code::MozartError>() {
                // Only print a message when there is one (bail_silent produces empty message).
                if !mozart_err.message.is_empty() {
                    eprintln!(
                        "{}",
                        mozart_core::console_format!("<error>{}</error>", mozart_err.message)
                    );
                }
                std::process::exit(mozart_err.exit_code);
            }

            // Generic anyhow error — print and exit with GENERAL_ERROR.
            eprintln!("{}", mozart_core::console_format!("<error>{e:#}</error>"));
            std::process::exit(exit_code::GENERAL_ERROR);
        }
    }
}