summaryrefslogtreecommitdiff
path: root/src/client.rs
blob: 97b4c614811a29b6065e03a11e6b8921a7dfbd59 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use log::debug;
use serde::Serialize;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::{tempdir, TempDir};

#[derive(Debug, thiserror::Error)]
pub enum ObnamClientError {
    #[error("failed to create temporary directory for server: {0}")]
    TempDir(std::io::Error),

    #[error("failed to write client configuration to {0}: {1}")]
    WriteConfig(PathBuf, std::io::Error),

    #[error("failed to run obnam: {0}")]
    Run(std::io::Error),
}

#[derive(Debug)]
pub struct ObnamClient {
    #[allow(dead_code)]
    tempdir: TempDir,
    #[allow(dead_code)]
    config: PathBuf,
}

impl ObnamClient {
    pub fn new(server_url: String, root: PathBuf) -> Result<Self, ObnamClientError> {
        debug!("creating ObnamClient");
        let tempdir = tempdir().map_err(ObnamClientError::TempDir)?;
        let config_filename = tempdir.path().join("client.yaml");

        let config = ClientConfig::new(server_url, root);
        config.write(&config_filename)?;

        Ok(Self {
            tempdir,
            config: config_filename,
        })
    }

    pub fn version() -> Result<String, ObnamClientError> {
        let output = Command::new("obnam")
            .arg("--version")
            .output()
            .map_err(ObnamClientError::Run)?;
        if output.status.code() != Some(0) {
            eprintln!("{}", String::from_utf8_lossy(&output.stdout));
            eprintln!("{}", String::from_utf8_lossy(&output.stderr));
            std::process::exit(1);
        }

        let v = String::from_utf8_lossy(&output.stdout);
        let v = v.strip_suffix('\n').or(Some(&v)).unwrap().to_string();
        Ok(v)
    }

    pub fn run(&self, args: &[&str]) -> Result<String, ObnamClientError> {
        let output = Command::new("obnam")
            .arg("--config")
            .arg(&self.config)
            .args(args)
            .output()
            .map_err(ObnamClientError::Run)?;
        if output.status.code() != Some(0) {
            eprintln!("{}", String::from_utf8_lossy(&output.stdout));
            eprintln!("{}", String::from_utf8_lossy(&output.stderr));
            std::process::exit(1);
        }

        Ok(String::from_utf8_lossy(&output.stdout)
            .to_owned()
            .to_string())
    }
}

#[derive(Debug, Serialize)]
pub struct ClientConfig {
    server_url: String,
    verify_tls_cert: bool,
    roots: Vec<PathBuf>,
    log: PathBuf,
}

impl ClientConfig {
    fn new(server_url: String, root: PathBuf) -> Self {
        Self {
            server_url,
            verify_tls_cert: false,
            roots: vec![root],
            log: PathBuf::from("obnam.log"),
        }
    }

    fn write(&self, filename: &Path) -> Result<(), ObnamClientError> {
        std::fs::write(filename, serde_yaml::to_string(self).unwrap())
            .map_err(|err| ObnamClientError::WriteConfig(filename.to_path_buf(), err))?;
        Ok(())
    }
}