diff options
Diffstat (limited to 'src/cmd.rs')
-rw-r--r-- | src/cmd.rs | 31 |
1 files changed, 27 insertions, 4 deletions
@@ -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<dyn crate::config::Config>, + config: &'static dyn Fn(config::Config) -> Box<dyn crate::config::Config>, 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<String> { |