From 5f10ab976fcc75683985d2551ff16f7858cac49a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 24 Jul 2019 21:32:21 +0200 Subject: [PATCH] Rewrite get_rtp_match() to not panic Signed-off-by: Matthias Beyer --- bin/core/imag/src/main.rs | 6 ++++- lib/core/libimagrt/src/runtime.rs | 40 ++++++++++++++++--------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/bin/core/imag/src/main.rs b/bin/core/imag/src/main.rs index 590c1bbc..9c9d817d 100644 --- a/bin/core/imag/src/main.rs +++ b/bin/core/imag/src/main.rs @@ -195,7 +195,11 @@ fn main() { let enable_logging = app.enable_logging(); let matches = app.matches(); - let rtp = ::libimagrt::runtime::get_rtp_match(&matches); + let rtp = ::libimagrt::runtime::get_rtp_match(&matches) + .unwrap_or_else(|e| { + trace_error(&e); + exit(1) + }); let configpath = matches .value_of(Runtime::arg_config_name()) .map_or_else(|| rtp.clone(), PathBuf::from); diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index 8845616d..43fa1eaf 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -79,7 +79,7 @@ impl<'a> Runtime<'a> { { let matches = cli_app.clone().matches(); - let rtp = get_rtp_match(&matches); + let rtp = get_rtp_match(&matches)?; let configpath = matches.value_of(Runtime::arg_config_name()) .map_or_else(|| rtp.clone(), PathBuf::from); @@ -122,7 +122,7 @@ impl<'a> Runtime<'a> { Runtime::init_logger(&matches, config.as_ref()) } - let rtp = get_rtp_match(&matches); + let rtp = get_rtp_match(&matches)?; let storepath = matches.value_of(Runtime::arg_storepath_name()) .map_or_else(|| { @@ -640,24 +640,26 @@ pub trait IdPathProvider { } /// Exported for the `imag` command, you probably do not want to use that. -pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> PathBuf { - matches.value_of(Runtime::arg_runtimepath_name()) - .map_or_else(|| { - if let Ok(home) = env::var("IMAG_RTP") { - return PathBuf::from(home); - } +pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> Result { + if let Some(p) = matches + .value_of(Runtime::arg_runtimepath_name()) + .map(PathBuf::from) + { + return Ok(p) + } - match env::var("HOME") { - Ok(home) => { - let mut p = PathBuf::from(home); - p.push(".imag"); - return p; - }, - Err(_) => panic!("You seem to be $HOME-less. Please get a $HOME before using this \ - software. We are sorry for you and hope you have some \ - accommodation anyways."), - } - }, PathBuf::from) + if let Ok(home) = env::var("IMAG_RTP") { + return Ok(PathBuf::from(home)) + } + + env::var("HOME") + .map(PathBuf::from) + .map(|mut p| { p.push(".imag"); p }) + .map_err(|_| { + err_msg("You seem to be $HOME-less. Please get a $HOME before using this \ + software. We are sorry for you and hope you have some \ + accommodation anyways.") + }) } fn get_override_specs(matches: &ArgMatches) -> Vec {