From fc8e8d37d397431d8cd9b4f85471cf7eb182b871 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 8 Jul 2023 10:32:56 +0200 Subject: [PATCH] refactor: remove some code duplication --- src/backup/manager/mod.rs | 5 +- src/backup/mod.rs | 2 - src/cli/backup.rs | 135 +++++++++++++++++--------------------- 3 files changed, 63 insertions(+), 79 deletions(-) diff --git a/src/backup/manager/mod.rs b/src/backup/manager/mod.rs index 6573376..4cf01f0 100644 --- a/src/backup/manager/mod.rs +++ b/src/backup/manager/mod.rs @@ -220,10 +220,7 @@ where } let mut enc = ar.into_inner()?; - enc.try_finish()?; - enc.finish()?; - - return Ok(()); + return enc.try_finish(); } } diff --git a/src/backup/mod.rs b/src/backup/mod.rs index f456c0b..8eecc0f 100644 --- a/src/backup/mod.rs +++ b/src/backup/mod.rs @@ -254,14 +254,12 @@ impl Backup { ar: &mut tar::Builder>, ) -> io::Result<()> { let mut own_ar = self.open(backup_dir)?; - // println!("{:?}", &state); for entry in own_ar.entries()? { let entry = entry?; let entry_path_in_tar = entry.path()?.to_path_buf(); if state.contains(&entry_path_in_tar) { - println!("{:?}", &entry_path_in_tar); let header = entry.header().clone(); ar.append(&header, entry)?; } diff --git a/src/cli/backup.rs b/src/cli/backup.rs index dccf8d9..3a993fc 100644 --- a/src/cli/backup.rs +++ b/src/cli/backup.rs @@ -4,7 +4,7 @@ use crate::other; use chrono::{TimeZone, Utc}; use clap::{Args, Subcommand}; use std::io; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; #[derive(Subcommand)] pub enum BackupCommands { @@ -86,6 +86,63 @@ impl BackupCreateArgs { } } +impl BackupListArgs { + pub fn run(&self, cli: &Cli) -> io::Result<()> { + let meta = cli.meta()?; + + // A bit scuffed? Sure + for (name, manager) in meta + .managers() + .iter() + .filter(|(name, _)| self.layer.is_none() || &self.layer.as_ref().unwrap() == name) + { + println!("{}", name); + + for chain in manager.chains().iter().filter(|c| !c.is_empty()) { + let mut iter = chain.iter(); + println!(" {}", iter.next().unwrap()); + + for backup in iter { + println!(" {}", backup); + } + } + } + + Ok(()) + } +} + +/// Tries to parse the given path as the path to a backup inside the backup directory with a +/// formatted timestamp. +fn parse_backup_path( + backup_dir: &Path, + backup_path: &Path, +) -> io::Result<(String, chrono::DateTime)> { + if !backup_path.starts_with(backup_dir) { + return Err(other("Provided file is not inside the backup directory.")); + } + + let layer = if let Some(parent) = backup_path.parent() { + // Backup files should be stored nested inside a layer's folder + if parent != backup_dir { + parent.file_name().unwrap().to_string_lossy() + } else { + return Err(other("Invalid path.")); + } + } else { + return Err(other("Invalid path.")); + }; + + let timestamp = if let Some(filename) = backup_path.file_name() { + Utc.datetime_from_str(&filename.to_string_lossy(), Backup::FILENAME_FORMAT) + .map_err(|_| other("Invalid filename."))? + } else { + return Err(other("Invalid filename.")); + }; + + Ok((layer.to_string(), timestamp)) +} + impl BackupRestoreArgs { pub fn run(&self, cli: &Cli) -> io::Result<()> { let backup_dir = cli.backup.canonicalize()?; @@ -100,29 +157,8 @@ impl BackupRestoreArgs { let output_worlds = self.output_worlds.canonicalize()?; // Parse input path - let path = self.path.canonicalize()?; - - if !path.starts_with(&backup_dir) { - return Err(other("Provided file is not inside the backup directory.")); - } - - let layer = if let Some(parent) = path.parent() { - // Backup files should be stored nested inside a layer's folder - if parent != backup_dir { - parent.file_name().unwrap().to_string_lossy() - } else { - return Err(other("Invalid path.")); - } - } else { - return Err(other("Invalid path.")); - }; - - let timestamp = if let Some(filename) = path.file_name() { - Utc.datetime_from_str(&filename.to_string_lossy(), Backup::FILENAME_FORMAT) - .map_err(|_| other("Invalid filename."))? - } else { - return Err(other("Invalid filename.")); - }; + let backup_path = self.path.canonicalize()?; + let (layer, timestamp) = parse_backup_path(&backup_dir, &backup_path)?; let meta = cli.meta()?; @@ -160,32 +196,6 @@ impl BackupRestoreArgs { } } -impl BackupListArgs { - pub fn run(&self, cli: &Cli) -> io::Result<()> { - let meta = cli.meta()?; - - // A bit scuffed? Sure - for (name, manager) in meta - .managers() - .iter() - .filter(|(name, _)| self.layer.is_none() || &self.layer.as_ref().unwrap() == name) - { - println!("{}", name); - - for chain in manager.chains().iter().filter(|c| !c.is_empty()) { - let mut iter = chain.iter(); - println!(" {}", iter.next().unwrap()); - - for backup in iter { - println!(" {}", backup); - } - } - } - - Ok(()) - } -} - impl BackupExportArgs { pub fn run(&self, cli: &Cli) -> io::Result<()> { let backup_dir = cli.backup.canonicalize()?; @@ -197,29 +207,8 @@ impl BackupExportArgs { } // Parse input path - let path = self.path.canonicalize()?; - - if !path.starts_with(&backup_dir) { - return Err(other("Provided file is not inside the backup directory.")); - } - - let layer = if let Some(parent) = path.parent() { - // Backup files should be stored nested inside a layer's folder - if parent != backup_dir { - parent.file_name().unwrap().to_string_lossy() - } else { - return Err(other("Invalid path.")); - } - } else { - return Err(other("Invalid path.")); - }; - - let timestamp = if let Some(filename) = path.file_name() { - Utc.datetime_from_str(&filename.to_string_lossy(), Backup::FILENAME_FORMAT) - .map_err(|_| other("Invalid filename."))? - } else { - return Err(other("Invalid filename.")); - }; + let backup_path = self.path.canonicalize()?; + let (layer, timestamp) = parse_backup_path(&backup_dir, &backup_path)?; let meta = cli.meta()?;