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 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);

View file

@ -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<PathBuf> {
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<String> {