Merge pull request #1314 from matthiasbeyer/imag/do-not-instantiate-runtime

Change "imag" impl to not instantiate Runtime object
This commit is contained in:
Matthias Beyer 2018-03-02 23:28:10 +01:00 committed by GitHub
commit c9af23f663
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 14 deletions

View file

@ -26,6 +26,7 @@ walkdir = "1"
log = "0.4.0" log = "0.4.0"
toml = "0.4" toml = "0.4"
toml-query = "0.6" toml-query = "0.6"
is-match = "0.1"
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }

View file

@ -22,6 +22,7 @@ extern crate clap;
extern crate walkdir; extern crate walkdir;
extern crate toml; extern crate toml;
extern crate toml_query; extern crate toml_query;
#[macro_use] extern crate is_match;
#[macro_use] extern crate libimagrt; #[macro_use] extern crate libimagrt;
extern crate libimagerror; extern crate libimagerror;
@ -33,16 +34,19 @@ use std::process::Stdio;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::io::{stdout, Stdout, Write}; use std::io::{stdout, Stdout, Write};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::path::PathBuf;
use walkdir::WalkDir; use walkdir::WalkDir;
use clap::{Arg, ArgMatches, AppSettings, SubCommand}; use clap::{Arg, ArgMatches, AppSettings, SubCommand};
use toml::Value; use toml::Value;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use libimagrt::error::RuntimeErrorKind;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error; use libimagrt::spec::CliSpec;
use libimagerror::io::ToExitCode; use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagerror::trace::trace_error;
/// Returns the helptext, putting the Strings in cmds as possible /// Returns the helptext, putting the Strings in cmds as possible
/// subcommands into it /// subcommands into it
@ -165,15 +169,23 @@ fn main() {
} }
} }
let rt = Runtime::new(app) let matches = app.matches();
.unwrap_or_else(|e| { let rtp = ::libimagrt::runtime::get_rtp_match(&matches);
let _ = writeln!(out, "Runtime couldn't be setup. Exiting") let configpath = matches
.to_exit_code() .value_of(Runtime::arg_config_name())
.unwrap_or_exit(); .map_or_else(|| rtp.clone(), PathBuf::from);
trace_error(&e); debug!("Config path = {:?}", configpath);
exit(1); let config = match ::libimagrt::configuration::fetch_config(&configpath) {
}); Ok(c) => Some(c),
let matches = rt.cli(); Err(e) => if !is_match!(e.kind(), &RuntimeErrorKind::ConfigNoConfigFileFound) {
trace_error(&e);
::std::process::exit(1)
} else {
println!("No config file found.");
println!("Continuing without configuration file");
None
},
};
debug!("matches: {:?}", matches); debug!("matches: {:?}", matches);
@ -214,7 +226,7 @@ fn main() {
exit(0); exit(0);
} }
let aliases = match fetch_aliases(&rt) { let aliases = match fetch_aliases(config.as_ref()) {
Ok(aliases) => aliases, Ok(aliases) => aliases,
Err(e) => { Err(e) => {
let _ = writeln!(out, "Error while fetching aliases from configuration file") let _ = writeln!(out, "Error while fetching aliases from configuration file")
@ -298,8 +310,8 @@ fn main() {
} }
} }
fn fetch_aliases(rt: &Runtime) -> Result<BTreeMap<String, String>, String> { fn fetch_aliases(config: Option<&Value>) -> Result<BTreeMap<String, String>, String> {
let cfg = rt.config().ok_or_else(|| String::from("No configuration found"))?; let cfg = config.ok_or_else(|| String::from("No configuration found"))?;
let value = cfg let value = cfg
.read("imag.aliases") .read("imag.aliases")
.map_err(|_| String::from("Reading from config failed")); .map_err(|_| String::from("Reading from config failed"));

View file

@ -440,7 +440,8 @@ impl<'a> Runtime<'a> {
} }
} }
fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> PathBuf { /// Exported for the `imag` command, you probably do not want to use that.
pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> PathBuf {
use std::env; use std::env;
matches.value_of(Runtime::arg_runtimepath_name()) matches.value_of(Runtime::arg_runtimepath_name())