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/backup.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'src/backup.rs') diff --git a/src/backup.rs b/src/backup.rs index 3d07ace..b468917 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -2,18 +2,18 @@ use std::{ fs::{create_dir_all, File}, io::{Read, Write}, path::PathBuf, - time::{SystemTime, UNIX_EPOCH}, + time::SystemTime, }; use serde::{Deserialize, Serialize}; -use tracing::info; +use tracing::{debug, info}; use uuid::Uuid; use crate::{ config::Config, error::{Error, Result}, - packages::{Manager, PackageList}, - pathinfo::PathInfo, + packages::PackageList, + pathinfo::PathInfo, send_notification, }; pub type Id = String; @@ -28,16 +28,20 @@ pub struct Backup { } impl Backup { - pub fn create(config: &Config, manager: Option) -> Result { + pub fn create(config: &Config) -> Result { let mut files: Vec = Vec::new(); for dir in &config.directories { files.push(PathInfo::from_path(config, dir)?); } Ok(Self { - // TODO: UUID not really needed, maybe a shorter hash id: Uuid::new_v4().to_string(), - timestamp: Self::get_timestamp(), - packages: Manager::get_manager(manager)?.get_installed()?, + timestamp: SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH)? + .as_secs(), + packages: config + .package_manager + .to_package_manager() + .get_installed()?, files, device: config.device.clone(), }) @@ -58,12 +62,14 @@ impl Backup { path.save(&backup_root)?; } + send_notification("Backup created" , "", notify_rust::Urgency::Normal)?; + Ok(()) } pub fn get_last(config: &Config) -> Result> { let backup_index_root = format!("{}/index.json", config.root); - info!(?backup_index_root, "backup index location:"); + debug!(?backup_index_root, "backup index location:"); let list: Vec = match Self::get_json_content(&backup_index_root) { Ok(list) => list, Err(err) => { @@ -74,8 +80,6 @@ impl Backup { } }; - info!(?list, "backup index:"); - Ok(Some(Self::from_index( config, &list.last().ok_or(Error::BackupNotFound)?.id, @@ -122,6 +126,8 @@ impl Backup { path.restore(config, &backup_root)?; } + send_notification("Backup restored" , "", notify_rust::Urgency::Normal)?; + Ok(()) } @@ -131,13 +137,6 @@ impl Backup { file.read_to_string(&mut content)?; Ok(serde_json::from_str(&content)?) } - - fn get_timestamp() -> u64 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_secs() - } } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -165,7 +164,7 @@ impl IndexEntry { f.write_all(&serde_json::to_vec(&loc)?)?; } else { - create_dir_all(&config.root).unwrap(); + create_dir_all(&config.root)?; let mut f = File::create(backup_index_root)?; f.write_all(&serde_json::to_vec(&vec![self])?)?; }; -- cgit v1.2.3