diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-04-10 09:31:03 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-04-10 10:29:27 +0300 |
commit | 3defb987dad68fa45dbf8a0044adb97104d04a0b (patch) | |
tree | 8d335215faf7b556961cd908d7d9b177e878938b /src/cmd/restore.rs | |
parent | 2aa0eef599db13db6802a75a8b285d75d8035033 (diff) | |
download | obnam2-3defb987dad68fa45dbf8a0044adb97104d04a0b.tar.gz |
refactor: struct Restore subcommand
Diffstat (limited to 'src/cmd/restore.rs')
-rw-r--r-- | src/cmd/restore.rs | 64 |
1 files changed, 41 insertions, 23 deletions
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)] |