summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-04-10 09:35:59 +0300
committerLars Wirzenius <liw@liw.fi>2021-04-10 10:29:27 +0300
commit20a5db2cc1b348087bca5e689ed38f98e1150955 (patch)
tree5c3cef3c30f43045c057c23dd162b5c9c02ceb6d
parent3defb987dad68fa45dbf8a0044adb97104d04a0b (diff)
downloadobnam2-20a5db2cc1b348087bca5e689ed38f98e1150955.tar.gz
refactor: struct GetChunk subcommand
-rw-r--r--src/bin/obnam.rs10
-rw-r--r--src/cmd/get_chunk.rs25
-rw-r--r--src/cmd/mod.rs4
3 files changed, 22 insertions, 17 deletions
diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs
index dcd09f6..a064bf9 100644
--- a/src/bin/obnam.rs
+++ b/src/bin/obnam.rs
@@ -3,12 +3,13 @@ use log::{debug, error, info, LevelFilter};
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Config, Logger, Root};
use obnam::cmd::backup::Backup;
+use obnam::cmd::get_chunk::GetChunk;
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_config;
use obnam::cmd::show_gen::ShowGeneration;
-use obnam::cmd::{get_chunk, show_config};
use obnam::config::ClientConfig;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
@@ -38,7 +39,7 @@ fn main() -> anyhow::Result<()> {
Command::ShowGeneration(x) => x.run(&config),
Command::ListFiles(x) => x.run(&config),
Command::Restore(x) => x.run(&config),
- Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id),
+ Command::GetChunk(x) => x.run(&config),
Command::Config => show_config(&config),
}
};
@@ -106,9 +107,6 @@ enum Command {
ListFiles(ListFiles),
Restore(Restore),
ShowGeneration(ShowGeneration),
- GetChunk {
- #[structopt()]
- chunk_id: String,
- },
+ GetChunk(GetChunk),
Config,
}
diff --git a/src/cmd/get_chunk.rs b/src/cmd/get_chunk.rs
index c9d640e..4ee70fe 100644
--- a/src/cmd/get_chunk.rs
+++ b/src/cmd/get_chunk.rs
@@ -3,14 +3,23 @@ use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use std::io::{stdout, Write};
+use structopt::StructOpt;
-pub fn get_chunk(config: &ClientConfig, chunk_id: &str) -> Result<(), ObnamError> {
- let client = BackupClient::new(config)?;
- let chunk_id: ChunkId = chunk_id.parse().unwrap();
- let chunk = client.fetch_chunk(&chunk_id)?;
+#[derive(Debug, StructOpt)]
+pub struct GetChunk {
+ #[structopt()]
+ chunk_id: String,
+}
+
+impl GetChunk {
+ pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> {
+ let client = BackupClient::new(config)?;
+ let chunk_id: ChunkId = self.chunk_id.parse().unwrap();
+ let chunk = client.fetch_chunk(&chunk_id)?;
- let stdout = stdout();
- let mut handle = stdout.lock();
- handle.write_all(chunk.data())?;
- Ok(())
+ let stdout = stdout();
+ let mut handle = stdout.lock();
+ handle.write_all(chunk.data())?;
+ Ok(())
+ }
}
diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs
index 3657d3e..02f4f01 100644
--- a/src/cmd/mod.rs
+++ b/src/cmd/mod.rs
@@ -1,12 +1,10 @@
pub mod backup;
+pub mod get_chunk;
pub mod init;
pub mod list;
pub mod list_files;
pub mod restore;
pub mod show_gen;
-pub mod get_chunk;
-pub use get_chunk::get_chunk;
-
pub mod show_config;
pub use show_config::show_config;