diff options
author | fxqnlr <[email protected]> | 2024-09-06 17:12:52 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-06 17:12:52 +0200 |
commit | 695556c3441f5ffd40c35387a5b45e4459684c2c (patch) | |
tree | 5a95edfc1c4b53b29220074e4598384390dd10a8 /src | |
parent | ab7f99e061e54924899b778e929dd2e17c8792d9 (diff) | |
download | arbs-695556c3441f5ffd40c35387a5b45e4459684c2c.tar arbs-695556c3441f5ffd40c35387a5b45e4459684c2c.tar.gz arbs-695556c3441f5ffd40c35387a5b45e4459684c2c.zip |
add get specific or last backup
Diffstat (limited to 'src')
-rw-r--r-- | src/backup.rs | 38 | ||||
-rw-r--r-- | src/error.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 8 |
3 files changed, 36 insertions, 13 deletions
diff --git a/src/backup.rs b/src/backup.rs index 69bc2ea..8cc94f1 100644 --- a/src/backup.rs +++ b/src/backup.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fs::{create_dir_all, File, OpenOptions}, | 2 | fs::{create_dir_all, File}, |
3 | io::{ErrorKind, Read, Write}, | 3 | io::{Read, Write}, |
4 | path::PathBuf, | 4 | path::PathBuf, |
5 | time::{SystemTime, UNIX_EPOCH}, | 5 | time::{SystemTime, UNIX_EPOCH}, |
6 | }; | 6 | }; |
@@ -43,7 +43,7 @@ impl Backup { | |||
43 | 43 | ||
44 | pub fn save(&self, config: &Config) -> Result<()> { | 44 | pub fn save(&self, config: &Config) -> Result<()> { |
45 | let rel_location = format!( | 45 | let rel_location = format!( |
46 | "bu_{}_{}", | 46 | "{}_{}", |
47 | gethostname() | 47 | gethostname() |
48 | .into_string() | 48 | .into_string() |
49 | .map_err(|_| Error::InvalidOsString)?, | 49 | .map_err(|_| Error::InvalidOsString)?, |
@@ -66,17 +66,35 @@ impl Backup { | |||
66 | Ok(()) | 66 | Ok(()) |
67 | } | 67 | } |
68 | 68 | ||
69 | pub fn get(config: &Config, _id: Option<BackupId>) -> Result<()> { | 69 | pub fn get_index(config: &Config, id: Option<BackupId>) -> Result<Self> { |
70 | let backup_index_root = format!("{}/index.json", config.root); | 70 | let backup_index_root = format!("{}/index.json", config.root); |
71 | let mut file = File::open(backup_index_root)?; | 71 | let list: Vec<BackupLocation> = Self::get_json_content(&backup_index_root)?; |
72 | let mut content = String::new(); | ||
73 | file.read_to_string(&mut content)?; | ||
74 | let list: Vec<BackupLocation> = serde_json::from_str(&content)?; | ||
75 | println!("{list:#?}"); | 72 | println!("{list:#?}"); |
76 | 73 | ||
77 | todo!(); | 74 | let index_loc = if let Some(id) = id { |
75 | list.iter() | ||
76 | .find(|bl| bl.id == id) | ||
77 | .ok_or(Error::BackupNotFound)? | ||
78 | .rel_location | ||
79 | .clone() | ||
80 | } else { | ||
81 | list.last() | ||
82 | .ok_or(Error::BackupNotFound)? | ||
83 | .rel_location | ||
84 | .clone() | ||
85 | }; | ||
78 | 86 | ||
79 | Ok(()) | 87 | let path = format!("{}/{index_loc}/index.json", config.root); |
88 | let index_file: Self = Self::get_json_content(&path)?; | ||
89 | |||
90 | Ok(index_file) | ||
91 | } | ||
92 | |||
93 | fn get_json_content<T: for<'a> Deserialize<'a>>(path: &str) -> Result<T> { | ||
94 | let mut file = File::open(path)?; | ||
95 | let mut content = String::new(); | ||
96 | file.read_to_string(&mut content)?; | ||
97 | Ok(serde_json::from_str(&content)?) | ||
80 | } | 98 | } |
81 | 99 | ||
82 | fn append_to_root_index(config: &Config, new_backup: BackupLocation) -> Result<()> { | 100 | fn append_to_root_index(config: &Config, new_backup: BackupLocation) -> Result<()> { |
diff --git a/src/error.rs b/src/error.rs index dc132f4..6afa3d0 100644 --- a/src/error.rs +++ b/src/error.rs | |||
@@ -20,6 +20,9 @@ pub enum Error { | |||
20 | #[error("OsString couldn't be converted to string")] | 20 | #[error("OsString couldn't be converted to string")] |
21 | InvalidOsString, | 21 | InvalidOsString, |
22 | 22 | ||
23 | #[error("Requested backup not found")] | ||
24 | BackupNotFound, | ||
25 | |||
23 | #[error("json: {source}")] | 26 | #[error("json: {source}")] |
24 | SerdeJson { | 27 | SerdeJson { |
25 | #[from] | 28 | #[from] |
diff --git a/src/main.rs b/src/main.rs index e0b3758..d5ccb75 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -21,11 +21,13 @@ fn main() -> color_eyre::Result<()> { | |||
21 | let pkgs = pacman.get_installed(); | 21 | let pkgs = pacman.get_installed(); |
22 | 22 | ||
23 | let backup = Backup::create(&cfg, pkgs)?; | 23 | let backup = Backup::create(&cfg, pkgs)?; |
24 | // println!("{backup:#?}"); | 24 | println!("{backup:#?}"); |
25 | 25 | ||
26 | backup.save(&cfg)?; | 26 | // backup.save(&cfg)?; |
27 | 27 | ||
28 | Backup::get(&cfg, None)?; | 28 | let index = Backup::get_index(&cfg, None)?; |
29 | |||
30 | println!("{index:#?}"); | ||
29 | 31 | ||
30 | // let fi = FileInfo::new("~/.config/nvim", &cfg)?; | 32 | // let fi = FileInfo::new("~/.config/nvim", &cfg)?; |
31 | // println!("{:?}", fi.get_absolute_path()); | 33 | // println!("{:?}", fi.get_absolute_path()); |