barkust 0.1.0

A Rust client library and CLI for the Bark push notification service
Documentation
use barkust::{BarkClient, Notification};
use clap::Parser;
use std::process;

#[derive(Parser, Debug)]
#[command(
    name = "barkust",
    about = "Send Bark push notifications from the command line"
)]
struct Args {
    /// Bark device key. Falls back to BARK_KEY environment variable.
    #[arg(short, long)]
    device_key: Option<String>,

    /// Bark server base URL.
    #[arg(short, long, default_value = "https://api.day.app")]
    url: String,

    /// Notification title.
    #[arg(short, long)]
    title: String,

    /// Notification body.
    #[arg(short, long)]
    body: String,

    /// Alert sound name.
    #[arg(long)]
    sound: Option<String>,

    /// Badge number.
    #[arg(long)]
    badge: Option<i32>,

    /// Notification group.
    #[arg(long)]
    group: Option<String>,

    /// URL to open on tap.
    #[arg(long)]
    open_url: Option<String>,

    /// Text to copy on tap.
    #[arg(long)]
    copy: Option<String>,

    /// Archive the notification in Bark history.
    #[arg(long)]
    archive: bool,

    /// Automatically copy the `copy` text when delivered.
    #[arg(long)]
    auto_copy: bool,

    /// Time-sensitive interruption level.
    #[arg(long)]
    time_sensitive: bool,
}

fn main() {
    // Ignore errors; .env is optional.
    let _ = dotenvy::dotenv();

    let args = Args::parse();

    let device_key = args
        .device_key
        .clone()
        .or_else(|| std::env::var("BARK_KEY").ok())
        .unwrap_or_else(|| {
            eprintln!("Error: device key is required. Provide --device-key or set BARK_KEY.");
            process::exit(1);
        });

    if let Err(e) = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(run(args, device_key))
    {
        eprintln!("Error: {e}");
        process::exit(1);
    }
}

async fn run(args: Args, device_key: String) -> Result<(), barkust::Error> {
    let client = BarkClient::with_url(&args.url, Some(device_key))?;

    let mut builder = Notification::builder()
        .title(args.title)
        .body(args.body)
        .archive(args.archive)
        .auto_copy(args.auto_copy);

    if let Some(sound) = args.sound {
        builder = builder.sound(sound);
    }
    if let Some(badge) = args.badge {
        builder = builder.badge(badge);
    }
    if let Some(group) = args.group {
        builder = builder.group(group);
    }
    if let Some(url) = args.open_url {
        builder = builder.url(url);
    }
    if let Some(copy) = args.copy {
        builder = builder.copy(copy);
    }
    if args.time_sensitive {
        builder = builder.level(barkust::Level::TimeSensitive);
    }

    let notification = builder.build();
    let response = client.send(&notification).await?;
    println!("Sent: {response:?}");
    Ok(())
}