aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-10-18 12:47:09 -0400
committerJesse Luehrs <doy@tozt.net>2019-10-18 12:59:35 -0400
commite5ccc1d13a82ec9b3bf86e61c045107b3171dfb2 (patch)
tree599866debbbfa453a3abce2df4d3278daa7e535e /src
parenta561217eca9f3076b097e6af3ee6cf229cf35668 (diff)
downloadteleterm-e5ccc1d13a82ec9b3bf86e61c045107b3171dfb2.tar.gz
teleterm-e5ccc1d13a82ec9b3bf86e61c045107b3171dfb2.zip
load config data from a file too
Diffstat (limited to 'src')
-rw-r--r--src/cmd.rs31
-rw-r--r--src/cmd/play.rs7
-rw-r--r--src/cmd/record.rs7
-rw-r--r--src/cmd/server.rs7
-rw-r--r--src/cmd/stream.rs7
-rw-r--r--src/cmd/watch.rs7
-rw-r--r--src/dirs.rs8
7 files changed, 60 insertions, 14 deletions
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<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> {
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<dyn crate::config::Config> {
- Box::new(Config::default())
+pub fn config(config: config::Config) -> Box<dyn crate::config::Config> {
+ 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<dyn crate::config::Config> {
- Box::new(Config::default())
+pub fn config(config: config::Config) -> Box<dyn crate::config::Config> {
+ 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<dyn crate::config::Config> {
- Box::new(Config::default())
+pub fn config(config: config::Config) -> Box<dyn crate::config::Config> {
+ 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<dyn crate::config::Config> {
- Box::new(Config::default())
+pub fn config(config: config::Config) -> Box<dyn crate::config::Config> {
+ 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<dyn crate::config::Config> {
- Box::new(Config::default())
+pub fn config(config: config::Config) -> Box<dyn crate::config::Config> {
+ 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()
}