summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs61
1 files changed, 53 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index 487d095..dad98c6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,8 +2,8 @@ use backup::Backup;
2use clap::Parser; 2use clap::Parser;
3use cli::Subcommands; 3use cli::Subcommands;
4use config::Config; 4use config::Config;
5use error::Error; 5use error::{Error, Result};
6use tracing::{debug, level_filters::LevelFilter}; 6use tracing::{debug, error, level_filters::LevelFilter};
7use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; 7use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
8 8
9mod backup; 9mod backup;
@@ -13,9 +13,7 @@ mod error;
13mod packages; 13mod packages;
14mod pathinfo; 14mod pathinfo;
15 15
16fn main() -> color_eyre::Result<()> { 16fn main() -> Result<()> {
17 color_eyre::install()?;
18
19 let file_appender = tracing_appender::rolling::never("./", "arbs.log"); 17 let file_appender = tracing_appender::rolling::never("./", "arbs.log");
20 let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); 18 let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
21 19
@@ -35,15 +33,27 @@ fn main() -> color_eyre::Result<()> {
35 ) 33 )
36 .init(); 34 .init();
37 debug!("logging initialized"); 35 debug!("logging initialized");
36
37 match run_cli() {
38 Ok(()) => { println!("OK") ; Ok(())},
39 Err(err) => {
40 error!(?err);
41 error!("{:?}", std::error::Error::source(&err));
42 send_notification("Backup Error", &err.to_string(), Urgency::Critical)?;
43 Err(err)
44 }
45 }
46}
38 47
48fn run_cli() -> Result<()> {
39 let cli = cli::Cli::parse(); 49 let cli = cli::Cli::parse();
40 50
41 let config = Config::load(cli.config)?; 51 let config = Config::load(cli.config)?;
42 52
43 match cli.subcommand { 53 match cli.subcommand {
44 Subcommands::GenerateConfig => Config::generate()?, 54 Subcommands::GenerateConfig => Config::generate()?,
45 Subcommands::Save { package_manager } => { 55 Subcommands::Save => {
46 let backup = Backup::create(&config, package_manager)?; 56 let backup = Backup::create(&config)?;
47 backup.save(&config)?; 57 backup.save(&config)?;
48 } 58 }
49 Subcommands::Restore { package_install } => { 59 Subcommands::Restore { package_install } => {
@@ -56,7 +66,42 @@ fn main() -> color_eyre::Result<()> {
56 } 66 }
57 67
58 last_backup.restore(&config)?; 68 last_backup.restore(&config)?;
59 } 69 },
60 }; 70 };
61 Ok(()) 71 Ok(())
62} 72}
73
74enum Urgency {
75 Normal,
76 Critical
77}
78
79#[cfg(feature = "notifications")]
80impl From<Urgency> for notify_rust::Urgency {
81 fn from(value: Urgency) -> Self {
82 match value {
83 Urgency::Normal => Self::Normal,
84 Urgency::Critical => Self::Critical,
85 }
86 }
87}
88
89fn send_notification(summary: &str, body: &str, urgency: Urgency) -> Result<()> {
90 #[cfg(feature = "notifications")]
91 {
92 let Some(mut icon) = dirs::data_dir() else {
93 return Err(Error::NoSysDir);
94 };
95 icon.push(env!("CARGO_PKG_NAME"));
96 icon.push("icon.png");
97
98 notify_rust::Notification::new()
99 .summary(summary)
100 .body(body)
101 .icon(&icon.to_string_lossy())
102 .timeout(0)
103 .urgency(urgency.into())
104 .show()?;
105 }
106 Ok(())
107}