From d44041040d755306c39d6de8da5b42d7ded6808c Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 25 Sep 2024 15:13:34 +0200 Subject: added notifications and improved stuff --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 487d095..ab23ab7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,9 @@ use backup::Backup; use clap::Parser; use cli::Subcommands; use config::Config; -use error::Error; -use tracing::{debug, level_filters::LevelFilter}; +use error::{Error, Result}; +use notify_rust::Urgency; +use tracing::{debug, error, level_filters::LevelFilter}; use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; mod backup; @@ -13,9 +14,7 @@ mod error; mod packages; mod pathinfo; -fn main() -> color_eyre::Result<()> { - color_eyre::install()?; - +fn main() -> Result<()> { let file_appender = tracing_appender::rolling::never("./", "arbs.log"); let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); @@ -35,15 +34,27 @@ fn main() -> color_eyre::Result<()> { ) .init(); debug!("logging initialized"); + + match run_cli() { + Ok(()) => { println!("OK") ; Ok(())}, + Err(err) => { + error!(?err); + error!("{:?}", std::error::Error::source(&err)); + send_notification("Backup Error", &err.to_string(), Urgency::Critical)?; + Err(err) + } + } +} +fn run_cli() -> Result<()> { let cli = cli::Cli::parse(); let config = Config::load(cli.config)?; match cli.subcommand { Subcommands::GenerateConfig => Config::generate()?, - Subcommands::Save { package_manager } => { - let backup = Backup::create(&config, package_manager)?; + Subcommands::Save => { + let backup = Backup::create(&config)?; backup.save(&config)?; } Subcommands::Restore { package_install } => { @@ -56,7 +67,27 @@ fn main() -> color_eyre::Result<()> { } last_backup.restore(&config)?; - } + }, }; Ok(()) } + +fn send_notification(summary: &str, body: &str, urgency: Urgency) -> Result<()> { + #[cfg(feature = "notifications")] + { + let Some(mut icon) = dirs::data_dir() else { + return Err(Error::NoSysDir); + }; + icon.push(env!("CARGO_PKG_NAME")); + icon.push("icon.png"); + + notify_rust::Notification::new() + .summary(summary) + .body(body) + .icon(&icon.to_string_lossy()) + .timeout(0) + .urgency(urgency) + .show()?; + } + Ok(()) +} -- cgit v1.2.3