barkust 0.2.1

A Rust client library and CLI for the Bark push notification service
Documentation
use barkust::{BarkClient, Level, 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>,

    /// Comma-separated list of device keys for batch push.
    #[arg(long, value_delimiter = ',')]
    device_keys: Option<Vec<String>>,

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

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

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

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

    /// Markdown-formatted content (overrides body on the client).
    #[arg(long)]
    markdown: Option<String>,

    /// Publicly accessible image URL.
    #[arg(long)]
    image: Option<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,

    /// Critical interruption level (ignores mute switch).
    #[arg(long)]
    critical: bool,

    /// Critical alert volume (0-10).
    #[arg(long)]
    volume: Option<i32>,

    /// Repeat the ringtone after delivery.
    #[arg(long)]
    call: bool,

    /// Identifier to update an existing notification.
    #[arg(long)]
    id: Option<String>,

    /// Delete the notification identified by `id`.
    #[arg(long)]
    delete: bool,

    /// Archive retention time in seconds.
    #[arg(long)]
    ttl: Option<i32>,
}

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))?;

    if args.body.is_none() && args.markdown.is_none() && !args.delete {
        eprintln!("Error: either --body or --markdown is required.");
        process::exit(1);
    }

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

    if let Some(body) = args.body {
        builder = builder.body(body);
    }
    if let Some(markdown) = args.markdown {
        builder = builder.markdown(markdown);
    }
    if let Some(image) = args.image {
        builder = builder.image(image);
    }
    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(Level::TimeSensitive);
    } else if args.critical {
        builder = builder.level(Level::Critical);
    }
    if let Some(volume) = args.volume {
        builder = builder.volume(volume);
    }
    if let Some(id) = args.id {
        builder = builder.id(id);
    }
    if let Some(ttl) = args.ttl {
        builder = builder.ttl(ttl);
    }

    let notification = builder.build();

    let response = if let Some(keys) = args.device_keys {
        client.send_batch(&notification, keys).await?
    } else {
        client.send(&notification).await?
    };

    println!("Sent: {response:?}");
    Ok(())
}