From 3defb987dad68fa45dbf8a0044adb97104d04a0b Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:31:03 +0300 Subject: refactor: struct Restore subcommand --- src/bin/obnam.rs | 13 ++++------- src/cmd/mod.rs | 4 +--- src/cmd/restore.rs | 64 ++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 0552e3b..dcd09f6 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -6,8 +6,9 @@ use obnam::cmd::backup::Backup; use obnam::cmd::init::Init; use obnam::cmd::list::List; use obnam::cmd::list_files::ListFiles; +use obnam::cmd::restore::Restore; use obnam::cmd::show_gen::ShowGeneration; -use obnam::cmd::{get_chunk, restore, show_config}; +use obnam::cmd::{get_chunk, show_config}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -36,7 +37,7 @@ fn main() -> anyhow::Result<()> { Command::List(x) => x.run(&config), Command::ShowGeneration(x) => x.run(&config), Command::ListFiles(x) => x.run(&config), - Command::Restore { gen_id, to } => restore(&config, &gen_id, &to), + Command::Restore(x) => x.run(&config), Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id), Command::Config => show_config(&config), } @@ -103,13 +104,7 @@ enum Command { Backup(Backup), List(List), ListFiles(ListFiles), - Restore { - #[structopt()] - gen_id: String, - - #[structopt(parse(from_os_str))] - to: PathBuf, - }, + Restore(Restore), ShowGeneration(ShowGeneration), GetChunk { #[structopt()] diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index a5f4884..3657d3e 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -2,10 +2,8 @@ pub mod backup; pub mod init; pub mod list; pub mod list_files; -pub mod show_gen; - pub mod restore; -pub use restore::restore; +pub mod show_gen; pub mod get_chunk; pub use get_chunk::get_chunk; diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index c2cbb7f..a321e80 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -18,32 +18,50 @@ use std::path::{Path, PathBuf}; use structopt::StructOpt; use tempfile::NamedTempFile; -pub fn restore(config: &ClientConfig, gen_ref: &str, to: &Path) -> Result<(), ObnamError> { - let temp = NamedTempFile::new()?; - - let client = BackupClient::new(config)?; - - let genlist = client.list_generations()?; - let gen_id: String = genlist.resolve(gen_ref)?; - info!("generation id is {}", gen_id); - - let gen = client.fetch_generation(&gen_id, temp.path())?; - info!("restoring {} files", gen.file_count()?); - let progress = create_progress_bar(gen.file_count()?, true); - for file in gen.files()? { - match file.reason() { - Reason::FileError => (), - _ => restore_generation(&client, &gen, file.fileno(), file.entry(), &to, &progress)?, +#[derive(Debug, StructOpt)] +pub struct Restore { + #[structopt()] + gen_id: String, + + #[structopt(parse(from_os_str))] + to: PathBuf, +} + +impl Restore { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let temp = NamedTempFile::new()?; + + let client = BackupClient::new(config)?; + + let genlist = client.list_generations()?; + let gen_id: String = genlist.resolve(&self.gen_id)?; + info!("generation id is {}", gen_id); + + let gen = client.fetch_generation(&gen_id, temp.path())?; + info!("restoring {} files", gen.file_count()?); + let progress = create_progress_bar(gen.file_count()?, true); + for file in gen.files()? { + match file.reason() { + Reason::FileError => (), + _ => restore_generation( + &client, + &gen, + file.fileno(), + file.entry(), + &self.to, + &progress, + )?, + } } - } - for file in gen.files()? { - if file.entry().is_dir() { - restore_directory_metadata(file.entry(), &to)?; + for file in gen.files()? { + if file.entry().is_dir() { + restore_directory_metadata(file.entry(), &self.to)?; + } } - } - progress.finish(); + progress.finish(); - Ok(()) + Ok(()) + } } #[derive(Debug, StructOpt)] -- cgit v1.2.1