|
|
|
@ -1,7 +1,6 @@ |
|
|
|
|
#[macro_use] |
|
|
|
|
extern crate log; |
|
|
|
|
pub extern crate log; |
|
|
|
|
|
|
|
|
|
use failure::{bail, Fallible}; |
|
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
|
use serde::Serialize; |
|
|
|
|
use std::collections::HashMap; |
|
|
|
@ -11,6 +10,9 @@ use std::io::Read; |
|
|
|
|
use std::path::{Path, PathBuf}; |
|
|
|
|
use std::str::FromStr; |
|
|
|
|
|
|
|
|
|
// re-export libs appearing in public API
|
|
|
|
|
pub use clap; |
|
|
|
|
|
|
|
|
|
pub const LOG_LEVELS: [&str; 6] = ["off", "error", "warn", "info", "debug", "trace"]; |
|
|
|
|
|
|
|
|
|
/// Implement this for the main config struct
|
|
|
|
@ -56,10 +58,10 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { |
|
|
|
|
|
|
|
|
|
/// Configure the config object using args.
|
|
|
|
|
/// Logging has already been inited and configured.
|
|
|
|
|
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> Fallible<Self::Init>; |
|
|
|
|
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> anyhow::Result<Self::Init>; |
|
|
|
|
|
|
|
|
|
/// Initialize the app
|
|
|
|
|
fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> Fallible<Self::Init> { |
|
|
|
|
fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> anyhow::Result<Self::Init> { |
|
|
|
|
let version = version.unwrap_or_else(|| env!("CARGO_PKG_VERSION")); |
|
|
|
|
let clap = clap::App::new(name) |
|
|
|
|
.arg( |
|
|
|
@ -160,7 +162,7 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { |
|
|
|
|
let mut level = config.logging(); |
|
|
|
|
|
|
|
|
|
if !LOG_LEVELS.contains(&level) { |
|
|
|
|
bail!("Invalid default log level: {}", level); |
|
|
|
|
anyhow::bail!("Invalid default log level: {}", level); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* env RUST_LOG overrides default if set, but can be changed by CLI args */ |
|
|
|
@ -169,7 +171,7 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { |
|
|
|
|
level = env_level; |
|
|
|
|
|
|
|
|
|
if !LOG_LEVELS.contains(&level) { |
|
|
|
|
bail!("Invalid env log level: {}", level); |
|
|
|
|
anyhow::bail!("Invalid env log level: {}", level); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -239,7 +241,7 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn read_file<P: AsRef<Path>>(path: P) -> Fallible<String> { |
|
|
|
|
fn read_file<P: AsRef<Path>>(path: P) -> anyhow::Result<String> { |
|
|
|
|
let path = path.as_ref(); |
|
|
|
|
let mut file = File::open(path)?; |
|
|
|
|
|
|
|
|
|