diff options
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..fb25009 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,84 @@ +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 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(()) + } +} |