summaryrefslogtreecommitdiff
path: root/src/cmd/get_chunk.rs
blob: 15614928144c59aac7fcab1ce8aae426e1fc7dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//! The `get-chunk` subcommand.

use crate::chunkid::ChunkId;
use crate::client::BackupClient;
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,
}

impl GetChunk {
    /// Run the command.
    pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> {
        let rt = Runtime::new()?;
        rt.block_on(self.run_async(config))
    }

    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(())
    }
}