diff options
-rw-r--r-- | src/cmd.rs | 11 | ||||
-rw-r--r-- | src/config.rs | 16 | ||||
-rw-r--r-- | src/error.rs | 3 |
3 files changed, 25 insertions, 5 deletions
@@ -52,7 +52,12 @@ pub fn parse<'a>() -> Result<clap::ArgMatches<'a>> { let mut app = clap::App::new(program_name()?) .about("Stream your terminal for other people to watch") .author(clap::crate_authors!()) - .version(clap::crate_version!()); + .version(clap::crate_version!()) + .arg( + clap::Arg::with_name("config-file") + .long("config-file") + .takes_value(true), + ); for cmd in COMMANDS { let subcommand = clap::SubCommand::with_name(cmd.name); @@ -77,7 +82,9 @@ pub fn run(matches: &clap::ArgMatches<'_>) -> Result<()> { ) .init(); - let config = crate::config::config()?; + let config = crate::config::config( + matches.value_of("config-file").map(std::path::Path::new), + )?; 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 5692c52..b954969 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,9 +36,19 @@ pub trait Config: std::fmt::Debug { fn run(&self) -> Result<()>; } -pub fn config() -> Result<Option<config::Config>> { - let config_filename = - crate::dirs::Dirs::new().config_file(CONFIG_FILENAME); +pub fn config( + filename: Option<&std::path::Path>, +) -> Result<Option<config::Config>> { + let config_filename = if let Some(filename) = filename { + if !filename.exists() { + return Err(Error::ConfigFileDoesntExist { + name: filename.to_string_lossy().to_string(), + }); + } + Some(filename.to_path_buf()) + } else { + crate::dirs::Dirs::new().config_file(CONFIG_FILENAME) + }; if let Some(config_filename) = config_filename { let mut config = config::Config::default(); config diff --git a/src/error.rs b/src/error.rs index cb231dc..2634472 100644 --- a/src/error.rs +++ b/src/error.rs @@ -22,6 +22,9 @@ pub enum Error { source: tokio::io::Error, }, + #[snafu(display("config file {} doesn't exist", name))] + ConfigFileDoesntExist { name: String }, + #[snafu(display("failed to connect to {}: {}", address, source))] Connect { address: std::net::SocketAddr, |