From 68340ff02e45198bd0e10eebbca665d2adf23a62 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 3 Mar 2021 10:44:54 +0200 Subject: feat: in errors about reading a configuration file, include its name --- src/bin/obnam-server.rs | 15 +++++++++++++-- src/bin/obnam.rs | 25 ++++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs index d4513fa..47f2b16 100644 --- a/src/bin/obnam-server.rs +++ b/src/bin/obnam-server.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use bytes::Bytes; use log::{debug, error, info}; use obnam::chunk::DataChunk; @@ -9,7 +10,7 @@ use serde::Serialize; use std::collections::HashMap; use std::default::Default; use std::net::{SocketAddr, ToSocketAddrs}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::Arc; use structopt::StructOpt; use tokio::sync::Mutex; @@ -28,7 +29,7 @@ async fn main() -> anyhow::Result<()> { pretty_env_logger::init(); let opt = Opt::from_args(); - let config = Config::read_config(&opt.config).unwrap(); + let config = load_config(&opt.config)?; let addresses: Vec = config.address.to_socket_addrs()?.collect(); if addresses.is_empty() { @@ -83,6 +84,16 @@ async fn main() -> anyhow::Result<()> { Ok(()) } +fn load_config(filename: &Path) -> Result { + let config = Config::read_config(&filename).with_context(|| { + format!( + "Couldn't read default configuration file {}", + filename.display() + ) + })?; + Ok(config) +} + pub async fn create_chunk( store: Arc>, meta: String, diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs index c163695..dd1b26a 100644 --- a/src/bin/obnam.rs +++ b/src/bin/obnam.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use log::{debug, error, info, LevelFilter}; use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Config, Logger, Root}; @@ -8,12 +9,9 @@ use structopt::StructOpt; fn main() -> anyhow::Result<()> { let opt = Opt::from_args(); - let config_file = match opt.config { - None => default_config(), - Some(ref path) => path.to_path_buf(), - }; - let config = ClientConfig::read_config(&config_file)?; + let config = load_config(&opt)?; setup_logging(&config.log)?; + debug!("configuration: {:#?}", config); info!("client starts"); debug!("{:?}", opt); @@ -38,6 +36,23 @@ fn main() -> anyhow::Result<()> { Ok(()) } +fn load_config(opt: &Opt) -> Result { + let config = match opt.config { + None => { + let filename = default_config(); + ClientConfig::read_config(&filename).with_context(|| { + format!( + "Couldn't read default configuration file {}", + filename.display() + ) + })? + } + Some(ref filename) => ClientConfig::read_config(&filename) + .with_context(|| format!("Couldn't read configuration file {}", filename.display()))?, + }; + Ok(config) +} + fn default_config() -> PathBuf { if let Some(path) = dirs::config_dir() { path.join("obnam").join("obnam.yaml") -- cgit v1.2.1 From 8f3b51d85eb3f192b23357b94a23f06ba2a7fff6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 3 Mar 2021 10:55:19 +0200 Subject: refactor: rename Config, ConfigError to show they are for server --- src/bin/obnam-server.rs | 8 ++++---- src/server.rs | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/bin/obnam-server.rs b/src/bin/obnam-server.rs index 47f2b16..ddbfce3 100644 --- a/src/bin/obnam-server.rs +++ b/src/bin/obnam-server.rs @@ -5,7 +5,7 @@ use obnam::chunk::DataChunk; use obnam::chunkid::ChunkId; use obnam::chunkmeta::ChunkMeta; use obnam::indexedstore::IndexedStore; -use obnam::server::{Config, ConfigError}; +use obnam::server::{ServerConfig, ServerConfigError}; use serde::Serialize; use std::collections::HashMap; use std::default::Default; @@ -35,7 +35,7 @@ async fn main() -> anyhow::Result<()> { if addresses.is_empty() { error!("specified address is empty set: {:?}", addresses); eprintln!("ERROR: server address is empty: {:?}", addresses); - return Err(ConfigError::BadServerAddress.into()); + return Err(ServerConfigError::BadServerAddress.into()); } let store = IndexedStore::new(&config.chunks)?; @@ -84,8 +84,8 @@ async fn main() -> anyhow::Result<()> { Ok(()) } -fn load_config(filename: &Path) -> Result { - let config = Config::read_config(&filename).with_context(|| { +fn load_config(filename: &Path) -> Result { + let config = ServerConfig::read_config(&filename).with_context(|| { format!( "Couldn't read default configuration file {}", filename.display() diff --git a/src/server.rs b/src/server.rs index 01a6958..2bd32ef 100644 --- a/src/server.rs +++ b/src/server.rs @@ -7,7 +7,7 @@ use std::default::Default; use std::path::{Path, PathBuf}; #[derive(Debug, Deserialize, Clone)] -pub struct Config { +pub struct ServerConfig { pub chunks: PathBuf, pub address: String, pub tls_key: PathBuf, @@ -15,7 +15,7 @@ pub struct Config { } #[derive(Debug, thiserror::Error)] -pub enum ConfigError { +pub enum ServerConfigError { #[error("Directory for chunks {0} does not exist")] ChunksDirNotFound(PathBuf), @@ -29,23 +29,23 @@ pub enum ConfigError { BadServerAddress, } -impl Config { - pub fn read_config(filename: &Path) -> anyhow::Result { +impl ServerConfig { + pub fn read_config(filename: &Path) -> anyhow::Result { let config = std::fs::read_to_string(filename)?; - let config: Config = serde_yaml::from_str(&config)?; + let config: Self = serde_yaml::from_str(&config)?; config.check()?; Ok(config) } pub fn check(&self) -> anyhow::Result<()> { if !self.chunks.exists() { - return Err(ConfigError::ChunksDirNotFound(self.chunks.clone()).into()); + return Err(ServerConfigError::ChunksDirNotFound(self.chunks.clone()).into()); } if !self.tls_cert.exists() { - return Err(ConfigError::TlsCertNotFound(self.tls_cert.clone()).into()); + return Err(ServerConfigError::TlsCertNotFound(self.tls_cert.clone()).into()); } if !self.tls_key.exists() { - return Err(ConfigError::TlsKeyNotFound(self.tls_key.clone()).into()); + return Err(ServerConfigError::TlsKeyNotFound(self.tls_key.clone()).into()); } Ok(()) } -- cgit v1.2.1