mirror of
https://gitlab.com/fabinfra/fabaccess/bffh.git
synced 2025-01-02 08:33:48 +01:00
Improve error messages on missing config
This commit is contained in:
parent
9346e433e1
commit
e3423c7786
@ -12,8 +12,8 @@ use crate::authorization::roles::Role;
|
|||||||
use crate::capnp::{Listen, TlsListen};
|
use crate::capnp::{Listen, TlsListen};
|
||||||
use crate::logging::LogConfig;
|
use crate::logging::LogConfig;
|
||||||
|
|
||||||
use std::path::Path;
|
|
||||||
use miette::IntoDiagnostic;
|
use miette::IntoDiagnostic;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct DhallConfig<'a> {
|
struct DhallConfig<'a> {
|
||||||
@ -36,23 +36,23 @@ pub struct MachineDescription {
|
|||||||
|
|
||||||
/// An optional description of the Machine.
|
/// An optional description of the Machine.
|
||||||
#[serde(
|
#[serde(
|
||||||
default,
|
default,
|
||||||
skip_serializing_if = "Option::is_none",
|
skip_serializing_if = "Option::is_none",
|
||||||
deserialize_with = "deser_option"
|
deserialize_with = "deser_option"
|
||||||
)]
|
)]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
|
||||||
#[serde(
|
#[serde(
|
||||||
default,
|
default,
|
||||||
skip_serializing_if = "Option::is_none",
|
skip_serializing_if = "Option::is_none",
|
||||||
deserialize_with = "deser_option"
|
deserialize_with = "deser_option"
|
||||||
)]
|
)]
|
||||||
pub wiki: Option<String>,
|
pub wiki: Option<String>,
|
||||||
|
|
||||||
#[serde(
|
#[serde(
|
||||||
default,
|
default,
|
||||||
skip_serializing_if = "Option::is_none",
|
skip_serializing_if = "Option::is_none",
|
||||||
deserialize_with = "deser_option"
|
deserialize_with = "deser_option"
|
||||||
)]
|
)]
|
||||||
pub category: Option<String>,
|
pub category: Option<String>,
|
||||||
|
|
||||||
@ -111,9 +111,9 @@ pub struct ModuleConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn deser_option<'de, D, T>(d: D) -> std::result::Result<Option<T>, D::Error>
|
pub(crate) fn deser_option<'de, D, T>(d: D) -> std::result::Result<Option<T>, D::Error>
|
||||||
where
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: serde::Deserializer<'de>,
|
||||||
T: serde::Deserialize<'de>,
|
T: serde::Deserialize<'de>,
|
||||||
{
|
{
|
||||||
Ok(T::deserialize(d).ok())
|
Ok(T::deserialize(d).ok())
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,50 @@
|
|||||||
mod dhall;
|
use std::path::Path;
|
||||||
pub use dhall::read_config_file as read;
|
|
||||||
pub use dhall::{Config, ModuleConfig, MachineDescription};
|
use miette::Diagnostic;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
pub(crate) use dhall::deser_option;
|
pub(crate) use dhall::deser_option;
|
||||||
|
pub use dhall::{Config, MachineDescription, ModuleConfig};
|
||||||
|
mod dhall;
|
||||||
|
|
||||||
struct ConfigBuilder;
|
#[derive(Debug, Error, Diagnostic)]
|
||||||
impl ConfigBuilder {
|
pub enum ConfigError {
|
||||||
|
#[error("The config file '{0}' does not exist or is not readable")]
|
||||||
|
#[diagnostic(
|
||||||
|
code(config::notfound),
|
||||||
|
help("Make sure the config file and the directory it's in are readable by the user running bffh")
|
||||||
|
)]
|
||||||
|
NotFound(String),
|
||||||
|
#[error("The path '{0}' does not point to a file")]
|
||||||
|
#[diagnostic(
|
||||||
|
code(config::notafile),
|
||||||
|
help("The config must be a file in the dhall format")
|
||||||
|
)]
|
||||||
|
NotAFile(String),
|
||||||
|
#[error("failed to parse config: {0}")]
|
||||||
|
#[diagnostic(code(config::parse))]
|
||||||
|
Parse(
|
||||||
|
#[from]
|
||||||
|
#[source]
|
||||||
|
serde_dhall::Error,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read(file: impl AsRef<Path>) -> Result<Config, ConfigError> {
|
||||||
|
let path = file.as_ref();
|
||||||
|
if !path.exists() {
|
||||||
|
return Err(ConfigError::NotFound(path.to_string_lossy().to_string()));
|
||||||
|
}
|
||||||
|
if !path.is_file() {
|
||||||
|
return Err(ConfigError::NotAFile(path.to_string_lossy().to_string()));
|
||||||
|
}
|
||||||
|
let mut config = dhall::read_config_file(file)?;
|
||||||
|
for (envvar, value) in std::env::vars() {
|
||||||
|
match envvar.as_str() {
|
||||||
|
// Do things like this?
|
||||||
|
// "BFFH_LOG" => config.logging.filter = Some(value),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
@ -135,7 +135,7 @@ fn main() -> miette::Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut config = config::read(&PathBuf::from_str(configpath).unwrap()).unwrap();
|
let mut config = config::read(&PathBuf::from_str(configpath).unwrap())?;
|
||||||
|
|
||||||
if matches.is_present("dump") {
|
if matches.is_present("dump") {
|
||||||
return Err(miette::miette!("DB Dumping is currently not implemented, except for the users db, using `--dump-users`"));
|
return Err(miette::miette!("DB Dumping is currently not implemented, except for the users db, using `--dump-users`"));
|
||||||
|
Loading…
Reference in New Issue
Block a user