aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-10-20 00:15:06 -0400
committerJesse Luehrs <doy@tozt.net>2019-10-20 00:52:52 -0400
commitfabf5bf86b3f7d63d9a47c12f91b756d67ea7f7c (patch)
tree47d9000a2089704fcd7870448711707711d2a75b
parenta1ffe44fd293bec84f56e858bec7a860ec643118 (diff)
downloadteleterm-fabf5bf86b3f7d63d9a47c12f91b756d67ea7f7c.tar.gz
teleterm-fabf5bf86b3f7d63d9a47c12f91b756d67ea7f7c.zip
a bit more refactoring
-rw-r--r--src/cmd.rs17
-rw-r--r--src/config.rs22
2 files changed, 23 insertions, 16 deletions
diff --git a/src/cmd.rs b/src/cmd.rs
index 838d3b1..c2d3d8e 100644
--- a/src/cmd.rs
+++ b/src/cmd.rs
@@ -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")]