From e5ccc1d13a82ec9b3bf86e61c045107b3171dfb2 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 18 Oct 2019 12:47:09 -0400 Subject: load config data from a file too --- src/cmd.rs | 31 +++++++++++++++++++++++++++---- src/cmd/play.rs | 7 +++++-- src/cmd/record.rs | 7 +++++-- src/cmd/server.rs | 7 +++++-- src/cmd/stream.rs | 7 +++++-- src/cmd/watch.rs | 7 +++++-- src/dirs.rs | 8 ++++++++ 7 files changed, 60 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/cmd.rs b/src/cmd.rs index fd6c689..14312be 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -9,7 +9,7 @@ mod watch; struct Command { name: &'static str, cmd: &'static dyn for<'a, 'b> Fn(clap::App<'a, 'b>) -> clap::App<'a, 'b>, - config: &'static dyn Fn() -> Box, + config: &'static dyn Fn(config::Config) -> Box, log_level: &'static str, } @@ -75,9 +75,32 @@ pub fn run(matches: &clap::ArgMatches<'_>) -> Result<()> { ) .init(); - let mut config = (chosen_cmd.config)(); - config.merge_args(chosen_submatches)?; - config.run() + let config_filename = crate::dirs::Dirs::new().config_file("config.toml"); + + let mut config = config::Config::default(); + if let Err(e) = config.merge(config::File::from(config_filename.clone())) + { + log::warn!( + "failed to read config file {}: {}", + config_filename.to_string_lossy(), + e + ); + // if merge returns an error, the config source will still have been + // added to the config object, so the config object will likely never + // work, so we should recreate it from scratch. + config = config::Config::default(); + } + // as far as i can tell, the Environment source can never actually fail. + // this is good because figuring out the logic to handle recreating the + // config object correctly (as per the previous comment) would be quite + // complicated. + config + .merge(config::Environment::with_prefix("TELETERM")) + .unwrap(); + + let mut cmd_config = (chosen_cmd.config)(config); + cmd_config.merge_args(chosen_submatches)?; + cmd_config.run() } fn program_name() -> Result { diff --git a/src/cmd/play.rs b/src/cmd/play.rs index a5fd534..bc8a214 100644 --- a/src/cmd/play.rs +++ b/src/cmd/play.rs @@ -43,8 +43,11 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { ) } -pub fn config() -> Box { - Box::new(Config::default()) +pub fn config(config: config::Config) -> Box { + Box::new(config.try_into().unwrap_or_else(|e| { + log::warn!("failed to parse config data: {}", e); + Config::default() + })) } #[allow(clippy::large_enum_variant)] diff --git a/src/cmd/record.rs b/src/cmd/record.rs index afdfcd6..08206b5 100644 --- a/src/cmd/record.rs +++ b/src/cmd/record.rs @@ -84,8 +84,11 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { .arg(clap::Arg::with_name("args").index(2).multiple(true)) } -pub fn config() -> Box { - Box::new(Config::default()) +pub fn config(config: config::Config) -> Box { + Box::new(config.try_into().unwrap_or_else(|e| { + log::warn!("failed to parse config data: {}", e); + Config::default() + })) } #[allow(clippy::large_enum_variant)] diff --git a/src/cmd/server.rs b/src/cmd/server.rs index ae96a86..c0e179d 100644 --- a/src/cmd/server.rs +++ b/src/cmd/server.rs @@ -142,8 +142,11 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { ) } -pub fn config() -> Box { - Box::new(Config::default()) +pub fn config(config: config::Config) -> Box { + Box::new(config.try_into().unwrap_or_else(|e| { + log::warn!("failed to parse config data: {}", e); + Config::default() + })) } fn create_server( diff --git a/src/cmd/stream.rs b/src/cmd/stream.rs index 2e1ea03..d52c410 100644 --- a/src/cmd/stream.rs +++ b/src/cmd/stream.rs @@ -172,8 +172,11 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { .arg(clap::Arg::with_name("args").index(2).multiple(true)) } -pub fn config() -> Box { - Box::new(Config::default()) +pub fn config(config: config::Config) -> Box { + Box::new(config.try_into().unwrap_or_else(|e| { + log::warn!("failed to parse config data: {}", e); + Config::default() + })) } struct StreamSession< diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index cde2fae..d17cf33 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -137,8 +137,11 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { .arg(clap::Arg::with_name("tls").long("tls")) } -pub fn config() -> Box { - Box::new(Config::default()) +pub fn config(config: config::Config) -> Box { + Box::new(config.try_into().unwrap_or_else(|e| { + log::warn!("failed to parse config data: {}", e); + Config::default() + })) } // XXX https://github.com/rust-lang/rust/issues/64362 diff --git a/src/dirs.rs b/src/dirs.rs index ac08f19..d868a4c 100644 --- a/src/dirs.rs +++ b/src/dirs.rs @@ -20,6 +20,14 @@ impl Dirs { Ok(()) } + fn config_dir(&self) -> &std::path::Path { + self.project_dirs.config_dir() + } + + pub fn config_file(&self, name: &str) -> std::path::PathBuf { + self.config_dir().join(name) + } + fn data_dir(&self) -> &std::path::Path { self.project_dirs.data_dir() } -- cgit v1.2.3-54-g00ecf