refactor: remove some code duplication
parent
5567323473
commit
fc8e8d37d3
|
@ -220,10 +220,7 @@ where
|
|||
}
|
||||
|
||||
let mut enc = ar.into_inner()?;
|
||||
enc.try_finish()?;
|
||||
enc.finish()?;
|
||||
|
||||
return Ok(());
|
||||
return enc.try_finish();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -254,14 +254,12 @@ impl<T: Clone> Backup<T> {
|
|||
ar: &mut tar::Builder<GzEncoder<File>>,
|
||||
) -> 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)?;
|
||||
}
|
||||
|
|
|
@ -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<Utc>)> {
|
||||
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()?;
|
||||
|
||||
|
|
Loading…
Reference in New Issue