fn main_and_drop_locals()

in focus/commands/src/cli/main.rs [1373:1446]


fn main_and_drop_locals() -> Result<ExitCode> {
    let started_at = Instant::now();
    let options = FocusOpts::parse();

    let FocusOpts {
        resolution_threads,
        working_directory,
        no_color,
        cmd: _,
    } = &options;

    if let Some(working_directory) = working_directory {
        std::env::set_current_dir(working_directory).context("Switching working directory")?;
    }

    let preserve_sandbox = true;

    let app = Arc::from(App::new(
        preserve_sandbox,
        Some(&feature_name_for(&options.cmd)),
        Some(env!("CARGO_PKG_NAME").to_owned()),
        Some(env!("CARGO_PKG_VERSION").to_owned()),
    )?);
    let ti_context = app.tool_insights_client();

    setup_thread_pool(*resolution_threads)?;

    let is_tty = termion::is_tty(&std::io::stdout());

    let sandbox_dir = app.sandbox().path().to_owned();
    let tracker = Tracker::from_config_dir()?;

    let _guard = focus_tracing::init_tracing(focus_tracing::TracingOpts {
        is_tty,
        no_color: *no_color,
        log_dir: Some(sandbox_dir.to_owned()),
    })?;

    info!(path = ?sandbox_dir, "Created sandbox");

    ensure_directories_exist(&tracker).context("Failed to create necessary directories")?;
    let setup_maintenance_task = thread::spawn({
        let options = options.clone();
        move || -> anyhow::Result<()> {
            setup_maintenance_scheduler(&options).context("Failed to setup maintenance scheduler")
        }
    });

    let exit_code = match run_subcommand(app.clone(), &tracker, options) {
        Ok(exit_code) => {
            ti_context
                .get_inner()
                .write_invocation_message(Some(0), None);
            exit_code
        }
        Err(e) => {
            ti_context
                .get_inner()
                .write_invocation_message(Some(1), None);
            return Err(e);
        }
    };

    sandbox::cleanup::run_with_default()?;
    setup_maintenance_task.join().unwrap()?;

    let total_runtime = started_at.elapsed();
    debug!(
        total_runtime_secs = total_runtime.as_secs_f32(),
        "Finished normally"
    );

    Ok(exit_code)
}