Rewrite get_rtp_match() to not panic

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-07-24 21:32:21 +02:00
parent 03fec5f4db
commit 5f10ab976f
2 changed files with 26 additions and 20 deletions

View file

@ -195,7 +195,11 @@ fn main() {
let enable_logging = app.enable_logging(); let enable_logging = app.enable_logging();
let matches = app.matches(); 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 let configpath = matches
.value_of(Runtime::arg_config_name()) .value_of(Runtime::arg_config_name())
.map_or_else(|| rtp.clone(), PathBuf::from); .map_or_else(|| rtp.clone(), PathBuf::from);

View file

@ -79,7 +79,7 @@ impl<'a> Runtime<'a> {
{ {
let matches = cli_app.clone().matches(); 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()) let configpath = matches.value_of(Runtime::arg_config_name())
.map_or_else(|| rtp.clone(), PathBuf::from); .map_or_else(|| rtp.clone(), PathBuf::from);
@ -122,7 +122,7 @@ impl<'a> Runtime<'a> {
Runtime::init_logger(&matches, config.as_ref()) 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()) let storepath = matches.value_of(Runtime::arg_storepath_name())
.map_or_else(|| { .map_or_else(|| {
@ -640,24 +640,26 @@ pub trait IdPathProvider {
} }
/// Exported for the `imag` command, you probably do not want to use that. /// Exported for the `imag` command, you probably do not want to use that.
pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> PathBuf { pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> Result<PathBuf> {
matches.value_of(Runtime::arg_runtimepath_name()) if let Some(p) = matches
.map_or_else(|| { .value_of(Runtime::arg_runtimepath_name())
if let Ok(home) = env::var("IMAG_RTP") { .map(PathBuf::from)
return PathBuf::from(home); {
return Ok(p)
} }
match env::var("HOME") { if let Ok(home) = env::var("IMAG_RTP") {
Ok(home) => { return Ok(PathBuf::from(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)
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<String> { fn get_override_specs(matches: &ArgMatches) -> Vec<String> {