From a9807c373e6a394b321fb268231a5b9e14eec19d Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Wed, 11 Jan 2023 22:59:05 +0100 Subject: [PATCH] Improve errors when opening a repo --- src/commands/rustic_config.rs | 9 ++++--- src/repository/mod.rs | 47 +++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/commands/rustic_config.rs b/src/commands/rustic_config.rs index b68d27b..81cef4e 100644 --- a/src/commands/rustic_config.rs +++ b/src/commands/rustic_config.rs @@ -1,6 +1,6 @@ use std::path::Path; -use anyhow::Result; +use anyhow::{Context, Result}; use directories::ProjectDirs; use merge::Merge; use serde::Deserialize; @@ -23,10 +23,11 @@ impl RusticConfig { // TODO: This should be log::info! - however, the logging config // can be stored in the config file and is needed to initialize the logger eprintln!("using config {}", path.display()); - let data = std::fs::read_to_string(path)?; - toml::from_str(&data)? + let data = std::fs::read_to_string(path).context("error reading config file")?; + toml::from_str(&data).context("error reading TOML from config file")? } else { - toml::from_str("")? + eprintln!("using no config file ({} doesn't exist)", path.display()); + Value::Array(Vec::new()) }; Ok(RusticConfig(config)) diff --git a/src/repository/mod.rs b/src/repository/mod.rs index c92d468..6d95aae 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -3,7 +3,7 @@ use std::io::BufReader; use std::path::PathBuf; use std::process::Command; -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use clap::Parser; use log::*; use merge::Merge; @@ -111,35 +111,58 @@ impl Repository { ) { (Some(pwd), _, _) => Ok(Some(pwd.clone())), (_, Some(file), _) => { - let mut file = BufReader::new(File::open(file)?); - Ok(Some(read_password_from_bufread(&mut file)?)) + let mut file = BufReader::new( + File::open(file) + .with_context(|| format!("error opening password file {file:?}"))?, + ); + Ok(Some( + read_password_from_bufread(&mut file).context("error reading password file")?, + )) } (_, _, Some(command)) => { let mut commands: Vec<_> = command.split(' ').collect(); let output = Command::new(commands[0]) .args(&mut commands[1..]) - .output()?; + .output() + .with_context(|| format!("failed to call password command {commands:?}"))?; let mut pwd = BufReader::new(&*output.stdout); - Ok(Some(read_password_from_bufread(&mut pwd)?)) + Ok(Some( + read_password_from_bufread(&mut pwd) + .context("error reading password from command")?, + )) } (None, None, None) => Ok(None), } } pub fn open(self) -> Result { - let config_ids = self.be.list(FileType::Config)?; + let config_ids = self + .be + .list(FileType::Config) + .context("error listing the repo config file")?; match config_ids.len() { 1 => {} // ok, continue - 0 => bail!("No config file found. Is there a repo at {}?", self.name), - _ => bail!("More than one config file at {}. Aborting.", self.name), + 0 => bail!( + "No repository config file found. Is there a repo at {}?", + self.name + ), + _ => bail!( + "More than one repository config file at {}. Aborting.", + self.name + ), } if let Some(be_hot) = &self.be_hot { - let mut keys = self.be.list_with_size(FileType::Key)?; + let mut keys = self + .be + .list_with_size(FileType::Key) + .context("error listing the repo keys")?; keys.sort_unstable_by_key(|key| key.0); - let mut hot_keys = be_hot.list_with_size(FileType::Key)?; + let mut hot_keys = be_hot + .list_with_size(FileType::Key) + .context("error listing the hot repo keys")?; hot_keys.sort_unstable_by_key(|key| key.0); if keys != hot_keys { bail!( @@ -153,7 +176,9 @@ impl Repository { info!("repository {}: password is correct.", self.name); let dbe = DecryptBackend::new(&self.be, key.clone()); - let config: ConfigFile = dbe.get_file(&config_ids[0])?; + let config: ConfigFile = dbe + .get_file(&config_ids[0]) + .context("error accessing config file")?; match (config.is_hot == Some(true), self.be_hot.is_some()) { (true, false) => bail!("repository is a hot repository!\nPlease use as --repo-hot in combination with the normal repo. Aborting."), (false, true) => bail!("repo-hot is not a hot repository! Aborting."),