diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-10-20 00:15:06 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-10-20 00:52:52 -0400 |
commit | fabf5bf86b3f7d63d9a47c12f91b756d67ea7f7c (patch) | |
tree | 47d9000a2089704fcd7870448711707711d2a75b | |
parent | a1ffe44fd293bec84f56e858bec7a860ec643118 (diff) | |
download | teleterm-fabf5bf86b3f7d63d9a47c12f91b756d67ea7f7c.tar.gz teleterm-fabf5bf86b3f7d63d9a47c12f91b756d67ea7f7c.zip |
a bit more refactoring
-rw-r--r-- | src/cmd.rs | 17 | ||||
-rw-r--r-- | src/config.rs | 22 |
2 files changed, 23 insertions, 16 deletions
@@ -75,22 +75,7 @@ pub fn run(matches: &clap::ArgMatches<'_>) -> Result<()> { ) .init(); - 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(); - } - + let config = crate::config::config(); let mut cmd_config = (chosen_cmd.config)(config); cmd_config.merge_args(chosen_submatches)?; log::debug!("{:?}", cmd_config); diff --git a/src/config.rs b/src/config.rs index b59bbcf..8fc2596 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,7 @@ use serde::de::Deserialize as _; use std::convert::TryFrom as _; use std::net::ToSocketAddrs as _; +const CONFIG_FILENAME: &str = "config.toml"; const DEFAULT_LISTEN_ADDRESS: &str = "127.0.0.1:4144"; const DEFAULT_CONNECT_ADDRESS: &str = "127.0.0.1:4144"; const DEFAULT_BUFFER_SIZE: usize = 4 * 1024 * 1024; @@ -21,6 +22,27 @@ pub trait Config: std::fmt::Debug { fn run(&self) -> Result<()>; } +pub fn config() -> config::Config { + let config_filename = + crate::dirs::Dirs::new().config_file(CONFIG_FILENAME); + + 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(); + } + + config +} + #[derive(serde::Deserialize, Debug)] pub struct Client { #[serde(deserialize_with = "auth_type", default = "default_auth_type")] |