From 7a32584448ebe00bdb45aa5daf506ce37c4811e0 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Apr 2021 09:22:45 +0300 Subject: refactor: struct ShowGeneration subcommand --- src/bin/obnam.rs | 10 ++++------ src/cmd/mod.rs | 4 +--- src/cmd/show_gen.rs | 49 +++++++++++++++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index 9e0ab0f..72dc208 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -5,7 +5,8 @@ use log4rs::config::{Appender, Config, Logger, Root}; use obnam::cmd::backup::Backup; use obnam::cmd::init::Init; use obnam::cmd::list::List; -use obnam::cmd::{get_chunk, list_files, restore, show_config, show_generation}; +use obnam::cmd::show_gen::ShowGeneration; +use obnam::cmd::{get_chunk, list_files, restore, show_config}; use obnam::config::ClientConfig; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -32,7 +33,7 @@ fn main() -> anyhow::Result<()> { Command::Init(_) => panic!("this cannot happen"), Command::Backup(x) => x.run(&config), Command::List(x) => x.run(&config), - Command::ShowGeneration { gen_id } => show_generation(&config, &gen_id), + Command::ShowGeneration(x) => x.run(&config), Command::ListFiles { gen_id } => list_files(&config, &gen_id), Command::Restore { gen_id, to } => restore(&config, &gen_id, &to), Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id), @@ -111,10 +112,7 @@ enum Command { #[structopt(parse(from_os_str))] to: PathBuf, }, - ShowGeneration { - #[structopt(default_value = "latest")] - gen_id: String, - }, + ShowGeneration(ShowGeneration), GetChunk { #[structopt()] chunk_id: String, diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 1f6bd78..f96dc2a 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,6 +1,7 @@ pub mod backup; pub mod init; pub mod list; +pub mod show_gen; mod list_files; pub use list_files::list_files; @@ -11,8 +12,5 @@ pub use restore::restore; pub mod get_chunk; pub use get_chunk::get_chunk; -pub mod show_gen; -pub use show_gen::show_generation; - pub mod show_config; pub use show_config::show_config; diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs index 5b786cf..ba39809 100644 --- a/src/cmd/show_gen.rs +++ b/src/cmd/show_gen.rs @@ -3,31 +3,40 @@ use crate::config::ClientConfig; use crate::error::ObnamError; use crate::fsentry::FilesystemKind; use indicatif::HumanBytes; +use structopt::StructOpt; use tempfile::NamedTempFile; -pub fn show_generation(config: &ClientConfig, gen_ref: &str) -> Result<(), ObnamError> { - let temp = NamedTempFile::new()?; +#[derive(Debug, StructOpt)] +pub struct ShowGeneration { + #[structopt(default_value = "latest")] + gen_id: String, +} + +impl ShowGeneration { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let temp = NamedTempFile::new()?; - let client = BackupClient::new(config)?; + let client = BackupClient::new(config)?; - let genlist = client.list_generations()?; - let gen_id: String = genlist.resolve(gen_ref)?; - let gen = client.fetch_generation(&gen_id, temp.path())?; - let files = gen.files()?; + let genlist = client.list_generations()?; + let gen_id: String = genlist.resolve(&self.gen_id)?; + let gen = client.fetch_generation(&gen_id, temp.path())?; + let files = gen.files()?; - let total_bytes = files.iter().fold(0, |acc, file| { - let e = file.entry(); - if e.kind() == FilesystemKind::Regular { - acc + file.entry().len() - } else { - acc - } - }); + let total_bytes = files.iter().fold(0, |acc, file| { + let e = file.entry(); + if e.kind() == FilesystemKind::Regular { + acc + file.entry().len() + } else { + acc + } + }); - println!("generation-id: {}", gen_id); - println!("file-count: {}", gen.file_count()?); - println!("file-bytes: {}", HumanBytes(total_bytes)); - println!("file-bytes-raw: {}", total_bytes); + println!("generation-id: {}", gen_id); + println!("file-count: {}", gen.file_count()?); + println!("file-bytes: {}", HumanBytes(total_bytes)); + println!("file-bytes-raw: {}", total_bytes); - Ok(()) + Ok(()) + } } -- cgit v1.2.1