Merge pull request #379 from rustic-rs/error-verbose

improve errors when opening a repo
This commit is contained in:
aawsome 2023-01-11 23:23:14 +01:00 committed by GitHub
commit ec1d79210e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 15 deletions

View File

@ -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))

View File

@ -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<OpenRepository> {
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."),