diff options
author | fxqnlr <[email protected]> | 2024-09-09 23:03:49 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-09 23:03:49 +0200 |
commit | 553bbac36bdc483135a7053ca64507e01397e5e1 (patch) | |
tree | 494dad0623628df4f1b86b93da51edf60795a901 /src/pathinfo.rs | |
parent | d396881816cd256cb12d03deebea445cba99ea85 (diff) | |
download | arbs-553bbac36bdc483135a7053ca64507e01397e5e1.tar arbs-553bbac36bdc483135a7053ca64507e01397e5e1.tar.gz arbs-553bbac36bdc483135a7053ca64507e01397e5e1.zip |
add package manager recognition
Diffstat (limited to 'src/pathinfo.rs')
-rw-r--r-- | src/pathinfo.rs | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/src/pathinfo.rs b/src/pathinfo.rs index 8b1ca2f..03b8a6b 100644 --- a/src/pathinfo.rs +++ b/src/pathinfo.rs | |||
@@ -120,7 +120,8 @@ impl PathInfo { | |||
120 | let old_path = modified_backup.get_absolute_file_location(config, &last_file.rel_location); | 120 | let old_path = modified_backup.get_absolute_file_location(config, &last_file.rel_location); |
121 | let new_path = format!("{location_root}/{rel_location}"); | 121 | let new_path = format!("{location_root}/{rel_location}"); |
122 | 122 | ||
123 | let mut old = File::open(old_path)?; let mut new = File::open(new_path)?; | 123 | let mut old = File::open(old_path)?; |
124 | let mut new = File::open(new_path)?; | ||
124 | 125 | ||
125 | let old_len = old.metadata()?.len(); | 126 | let old_len = old.metadata()?.len(); |
126 | let new_len = new.metadata()?.len(); | 127 | let new_len = new.metadata()?.len(); |
@@ -195,12 +196,9 @@ impl PathInfo { | |||
195 | return Err(Error::InvalidDirectory(value.to_string())); | 196 | return Err(Error::InvalidDirectory(value.to_string())); |
196 | }; | 197 | }; |
197 | if split.0.starts_with('~') { | 198 | if split.0.starts_with('~') { |
198 | if config.user.len() != 1 { | ||
199 | return Err(Error::MultiUser); | ||
200 | } | ||
201 | return Ok(( | 199 | return Ok(( |
202 | split.1.to_string(), | 200 | split.1.to_string(), |
203 | LocationRoot::User(config.user[0].clone()), | 201 | LocationRoot::User, |
204 | )); | 202 | )); |
205 | }; | 203 | }; |
206 | Ok(( | 204 | Ok(( |
@@ -212,18 +210,22 @@ impl PathInfo { | |||
212 | 210 | ||
213 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | 211 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
214 | pub enum LocationRoot { | 212 | pub enum LocationRoot { |
215 | User(String), | 213 | User, |
216 | Custom(String), | 214 | Custom(String), |
217 | SystemSettings, | 215 | SystemConfig, |
216 | UserConfig, | ||
218 | Root, | 217 | Root, |
219 | } | 218 | } |
220 | 219 | ||
221 | impl Display for LocationRoot { | 220 | impl Display for LocationRoot { |
222 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 221 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
223 | match self { | 222 | match self { |
224 | LocationRoot::User(user) => write!(f, "/home/{user}"), | 223 | LocationRoot::User => write!(f, "{}", dirs::home_dir().unwrap().to_string_lossy()), |
225 | LocationRoot::Custom(loc) => write!(f, "{loc}"), | 224 | LocationRoot::Custom(loc) => write!(f, "{loc}"), |
226 | LocationRoot::SystemSettings => write!(f, "/etc"), | 225 | LocationRoot::SystemConfig => write!(f, "/etc"), |
226 | LocationRoot::UserConfig => { | ||
227 | write!(f, "{}", dirs::config_local_dir().unwrap().to_string_lossy()) | ||
228 | } | ||
227 | LocationRoot::Root => write!(f, "/"), | 229 | LocationRoot::Root => write!(f, "/"), |
228 | } | 230 | } |
229 | } | 231 | } |
@@ -236,8 +238,9 @@ impl LocationRoot { | |||
236 | return Err(Error::NoIndex); | 238 | return Err(Error::NoIndex); |
237 | }; | 239 | }; |
238 | match split_op.0 { | 240 | match split_op.0 { |
239 | "u" => Ok(Self::User(split_op.1.to_string())), | 241 | "u" => Ok(Self::User), |
240 | "s" => Ok(Self::SystemSettings), | 242 | "s" => Ok(Self::SystemConfig), |
243 | "d" => Ok(Self::UserConfig), | ||
241 | "r" => Ok(Self::Root), | 244 | "r" => Ok(Self::Root), |
242 | "c" => Ok(Self::Custom( | 245 | "c" => Ok(Self::Custom( |
243 | config | 246 | config |
@@ -262,7 +265,6 @@ mod tests { | |||
262 | backup::Backup, | 265 | backup::Backup, |
263 | config::Config, | 266 | config::Config, |
264 | error::{Error, Result}, | 267 | error::{Error, Result}, |
265 | packages::{pacman::Pacman, PackageManager}, | ||
266 | }; | 268 | }; |
267 | 269 | ||
268 | use super::LocationRoot; | 270 | use super::LocationRoot; |
@@ -276,8 +278,8 @@ mod tests { | |||
276 | .insert("test".to_string(), "/usr/local/test".to_string()); | 278 | .insert("test".to_string(), "/usr/local/test".to_string()); |
277 | 279 | ||
278 | let mut values_ok: Vec<(&str, LocationRoot)> = Vec::new(); | 280 | let mut values_ok: Vec<(&str, LocationRoot)> = Vec::new(); |
279 | values_ok.push(("u:test", LocationRoot::User("test".to_string()))); | 281 | values_ok.push(("u:test", LocationRoot::User)); |
280 | values_ok.push(("s:", LocationRoot::SystemSettings)); | 282 | values_ok.push(("s:", LocationRoot::SystemConfig)); |
281 | values_ok.push(("r:", LocationRoot::Root)); | 283 | values_ok.push(("r:", LocationRoot::Root)); |
282 | values_ok.push(( | 284 | values_ok.push(( |
283 | "c:test", | 285 | "c:test", |
@@ -321,7 +323,6 @@ mod tests { | |||
321 | #[test] | 323 | #[test] |
322 | fn parse_location() -> Result<()> { | 324 | fn parse_location() -> Result<()> { |
323 | let mut config = Config::default(); | 325 | let mut config = Config::default(); |
324 | config.user.push("test".to_string()); | ||
325 | config | 326 | config |
326 | .custom_directories | 327 | .custom_directories |
327 | .insert("test".to_string(), "/usr/local/test".to_string()); | 328 | .insert("test".to_string(), "/usr/local/test".to_string()); |
@@ -331,14 +332,14 @@ mod tests { | |||
331 | "~/.config/nvim", | 332 | "~/.config/nvim", |
332 | ( | 333 | ( |
333 | ".config/nvim".to_string(), | 334 | ".config/nvim".to_string(), |
334 | LocationRoot::User("test".to_string()), | 335 | LocationRoot::User, |
335 | ), | 336 | ), |
336 | )); | 337 | )); |
337 | values_ok.push(( | 338 | values_ok.push(( |
338 | "u:test/.config/nvim", | 339 | "u:test/.config/nvim", |
339 | ( | 340 | ( |
340 | ".config/nvim".to_string(), | 341 | ".config/nvim".to_string(), |
341 | LocationRoot::User("test".to_string()), | 342 | LocationRoot::User, |
342 | ), | 343 | ), |
343 | )); | 344 | )); |
344 | values_ok.push(( | 345 | values_ok.push(( |
@@ -351,7 +352,7 @@ mod tests { | |||
351 | )); | 352 | )); |
352 | values_ok.push(( | 353 | values_ok.push(( |
353 | "s:/.config/nvim", | 354 | "s:/.config/nvim", |
354 | (".config/nvim".to_string(), LocationRoot::SystemSettings), | 355 | (".config/nvim".to_string(), LocationRoot::SystemConfig), |
355 | )); | 356 | )); |
356 | values_ok.push(( | 357 | values_ok.push(( |
357 | "c:test/.config/nvim", | 358 | "c:test/.config/nvim", |
@@ -385,9 +386,7 @@ mod tests { | |||
385 | let mut f = File::create("./backup-test-dir/nothing.txt")?; | 386 | let mut f = File::create("./backup-test-dir/nothing.txt")?; |
386 | f.write_all("unmodified".as_bytes())?; | 387 | f.write_all("unmodified".as_bytes())?; |
387 | 388 | ||
388 | let pacman = Pacman; | 389 | let backup = Backup::create(&config, None)?; |
389 | let pkgs = pacman.get_installed()?; | ||
390 | let backup = Backup::create(&config, pkgs)?; | ||
391 | backup.save(&config)?; | 390 | backup.save(&config)?; |
392 | 391 | ||
393 | let mut f = File::create("./backup-test-dir/size.txt")?; | 392 | let mut f = File::create("./backup-test-dir/size.txt")?; |