free-launch 0.2.8

A simple fuzzy launcher written in Rust.
Documentation
use clap::Parser;
use free_launch::free_launch::free_launch::FreeLaunch;
use free_launch::free_launch::free_launch::PROJECT_DIRS;
use std::{fs, result::Result::Ok};
use tracing::{error, info};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

/// A simple fuzzy launcher written in Rust
#[derive(Parser, Debug)]
#[command(author, about, long_about = None)]
#[command(version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("GIT_COMMIT_HASH"), ")"))]
struct Args {}

// TODO return errors from this function
fn setup_tracing() {
    // Get XDG-compliant data directory
    let log_dir = PROJECT_DIRS.data_dir();

    // Create log directory if it doesn't exist
    fs::create_dir_all(log_dir).expect("log dir to exist");

    // Setup file appender
    let file_appender = tracing_appender::rolling::daily(log_dir, "free-launch.log");
    let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);

    // Initialize tracing subscriber
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer()
                .with_writer(non_blocking)
                .with_ansi(false),
        )
        .with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")),
        )
        .init();

    // Keep the guard alive for the duration of the program
    std::mem::forget(_guard);
}

fn main() -> Result<(), eframe::Error> {
    // Parse command line arguments (handles --help and --version automatically)
    let _args = Args::parse();

    // Setup logging
    setup_tracing();

    let result = match existing_instance::establish_endpoint("basic_example", false) {
        Ok(endpoint) => match endpoint {
            existing_instance::Endpoint::New(listener) => {
                info!("Starting free-launch application...");
                FreeLaunch::run(listener)
            }
            existing_instance::Endpoint::Existing(mut stream) => {
                info!("free-launch already running, sending focus request...");
                // Nudge the existing instance to cause it to focus the window
                stream.send(existing_instance::Msg::Nudge);
                Ok(())
            }
        },
        Err(error) => {
            error!("Application exited with error: {}", error);
            // TODO switch to returning an error here
            Ok(())
        }
    };

    // TODO we probably want to remove this section
    match &result {
        Ok(_) => info!("Application exited successfully"),
        Err(e) => error!("Application exited with error: {}", e),
    }

    result
}