aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-10-20 13:55:28 -0400
committerJesse Luehrs <doy@tozt.net>2019-10-20 13:59:35 -0400
commitca7de0c239091b6c3d71e1863baf885754d4dc54 (patch)
treeee7898ce7401f8c3c4d6a410e04a1013bdb1eac5
parent4d8684f14658e404e25bac8235340129920e5017 (diff)
downloadteleterm-ca7de0c239091b6c3d71e1863baf885754d4dc54.tar.gz
teleterm-ca7de0c239091b6c3d71e1863baf885754d4dc54.zip
make running with no config file work again
-rw-r--r--src/cmd.rs4
-rw-r--r--src/cmd/play.rs12
-rw-r--r--src/cmd/record.rs12
-rw-r--r--src/cmd/server.rs12
-rw-r--r--src/cmd/stream.rs12
-rw-r--r--src/cmd/watch.rs12
-rw-r--r--src/config.rs26
-rw-r--r--src/error.rs3
8 files changed, 53 insertions, 40 deletions
diff --git a/src/cmd.rs b/src/cmd.rs
index b0947d5..e03d59a 100644
--- a/src/cmd.rs
+++ b/src/cmd.rs
@@ -10,7 +10,7 @@ struct Command {
name: &'static str,
cmd: &'static dyn for<'a, 'b> Fn(clap::App<'a, 'b>) -> clap::App<'a, 'b>,
config: &'static dyn Fn(
- config::Config,
+ Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>>,
log_level: &'static str,
}
@@ -77,7 +77,7 @@ pub fn run(matches: &clap::ArgMatches<'_>) -> Result<()> {
)
.init();
- let config = crate::config::config();
+ 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/cmd/play.rs b/src/cmd/play.rs
index 2bb95c2..4f8994d 100644
--- a/src/cmd/play.rs
+++ b/src/cmd/play.rs
@@ -29,11 +29,15 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
}
pub fn config(
- config: config::Config,
+ config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
- let config: Config = config
- .try_into()
- .context(crate::error::CouldntParseConfig)?;
+ let config: Config = if let Some(config) = config {
+ config
+ .try_into()
+ .context(crate::error::CouldntParseConfig)?
+ } else {
+ Config::default()
+ };
Ok(Box::new(config))
}
diff --git a/src/cmd/record.rs b/src/cmd/record.rs
index b301fed..4f3f4dd 100644
--- a/src/cmd/record.rs
+++ b/src/cmd/record.rs
@@ -41,11 +41,15 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
}
pub fn config(
- config: config::Config,
+ config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
- let config: Config = config
- .try_into()
- .context(crate::error::CouldntParseConfig)?;
+ let config: Config = if let Some(config) = config {
+ config
+ .try_into()
+ .context(crate::error::CouldntParseConfig)?
+ } else {
+ Config::default()
+ };
Ok(Box::new(config))
}
diff --git a/src/cmd/server.rs b/src/cmd/server.rs
index a6b8cff..86635ee 100644
--- a/src/cmd/server.rs
+++ b/src/cmd/server.rs
@@ -67,11 +67,15 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
}
pub fn config(
- config: config::Config,
+ config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
- let config: Config = config
- .try_into()
- .context(crate::error::CouldntParseConfig)?;
+ let config: Config = if let Some(config) = config {
+ config
+ .try_into()
+ .context(crate::error::CouldntParseConfig)?
+ } else {
+ Config::default()
+ };
Ok(Box::new(config))
}
diff --git a/src/cmd/stream.rs b/src/cmd/stream.rs
index 44f09d7..9555f47 100644
--- a/src/cmd/stream.rs
+++ b/src/cmd/stream.rs
@@ -95,11 +95,15 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
}
pub fn config(
- config: config::Config,
+ config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
- let config: Config = config
- .try_into()
- .context(crate::error::CouldntParseConfig)?;
+ let config: Config = if let Some(config) = config {
+ config
+ .try_into()
+ .context(crate::error::CouldntParseConfig)?
+ } else {
+ Config::default()
+ };
Ok(Box::new(config))
}
diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs
index d63cf13..6806a0e 100644
--- a/src/cmd/watch.rs
+++ b/src/cmd/watch.rs
@@ -87,11 +87,15 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
}
pub fn config(
- config: config::Config,
+ config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
- let config: Config = config
- .try_into()
- .context(crate::error::CouldntParseConfig)?;
+ let config: Config = if let Some(config) = config {
+ config
+ .try_into()
+ .context(crate::error::CouldntParseConfig)?
+ } else {
+ Config::default()
+ };
Ok(Box::new(config))
}
diff --git a/src/config.rs b/src/config.rs
index 10b358b..5692c52 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -36,28 +36,18 @@ pub trait Config: std::fmt::Debug {
fn run(&self) -> Result<()>;
}
-pub fn config() -> config::Config {
+pub fn config() -> Result<Option<config::Config>> {
let config_filename =
crate::dirs::Dirs::new().config_file(CONFIG_FILENAME);
-
- let mut config = config::Config::default();
if let Some(config_filename) = config_filename {
- 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 mut config = config::Config::default();
+ config
+ .merge(config::File::from(config_filename))
+ .context(crate::error::ParseConfigFile)?;
+ Ok(Some(config))
+ } else {
+ Ok(None)
}
-
- config
}
#[derive(serde::Deserialize, Debug)]
diff --git a/src/error.rs b/src/error.rs
index a02b836..cb231dc 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -179,6 +179,9 @@ pub enum Error {
source: std::num::ParseIntError,
},
+ #[snafu(display("failed to parse config file: {}", source))]
+ ParseConfigFile { source: config::ConfigError },
+
#[snafu(display("failed to parse incoming http request"))]
ParseHttpRequest,