summaryrefslogtreecommitdiff
path: root/src/cmd/resolve.rs
blob: 12432cc7a3fd4834f4c4bd3668d5452130a4ea73 (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
35
36
37
38
39
40
41
42
43
44
//! The `resolve` subcommand.

use crate::chunk::ClientTrust;
use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use structopt::StructOpt;
use tokio::runtime::Runtime;

/// Resolve a generation reference into a generation id.
#[derive(Debug, StructOpt)]
pub struct Resolve {
    /// The generation reference.
    generation: String,
}

impl Resolve {
    /// 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 trust = client
            .get_client_trust()
            .await?
            .or_else(|| Some(ClientTrust::new("FIXME", None, "".to_string(), vec![])))
            .unwrap();
        let generations = client.list_generations(&trust);

        match generations.resolve(&self.generation) {
            Err(err) => {
                return Err(err.into());
            }
            Ok(gen_id) => {
                println!("{}", gen_id.as_chunk_id());
            }
        };

        Ok(())
    }
}