diff options
Diffstat (limited to 'src/cmd/get_chunk.rs')
-rw-r--r-- | src/cmd/get_chunk.rs | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/cmd/get_chunk.rs b/src/cmd/get_chunk.rs index bf653ff..1561492 100644 --- a/src/cmd/get_chunk.rs +++ b/src/cmd/get_chunk.rs @@ -1,15 +1,34 @@ +//! The `get-chunk` subcommand. + use crate::chunkid::ChunkId; use crate::client::BackupClient; -use crate::client::ClientConfig; +use crate::config::ClientConfig; +use crate::error::ObnamError; +use clap::Parser; use std::io::{stdout, Write}; +use tokio::runtime::Runtime; + +/// Fetch a chunk from the server. +#[derive(Debug, Parser)] +pub struct GetChunk { + /// Identifier of chunk to fetch. + chunk_id: String, +} -pub fn get_chunk(config: &ClientConfig, chunk_id: &str) -> anyhow::Result<()> { - let client = BackupClient::new(&config.server_url)?; - let chunk_id: ChunkId = chunk_id.parse().unwrap(); - let chunk = client.fetch_chunk(&chunk_id)?; +impl GetChunk { + /// Run the command. + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let rt = Runtime::new()?; + rt.block_on(self.run_async(config)) + } - let stdout = stdout(); - let mut handle = stdout.lock(); - handle.write_all(chunk.data())?; - Ok(()) + async fn run_async(&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).await?; + let stdout = stdout(); + let mut handle = stdout.lock(); + handle.write_all(chunk.data())?; + Ok(()) + } } |