summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-04-10 09:22:45 +0300
committerLars Wirzenius <liw@liw.fi>2021-04-10 10:29:27 +0300
commit7a32584448ebe00bdb45aa5daf506ce37c4811e0 (patch)
tree33249e98feb9f38b66d8961df007589798d875e4
parentc196a68471dd9dafbcc4030342aa4164d7a2d3e3 (diff)
downloadobnam2-7a32584448ebe00bdb45aa5daf506ce37c4811e0.tar.gz
refactor: struct ShowGeneration subcommand
-rw-r--r--src/bin/obnam.rs10
-rw-r--r--src/cmd/mod.rs4
-rw-r--r--src/cmd/show_gen.rs49
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(())
+ }
}