summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-03-02 23:28:10 +0100
committerGitHub <noreply@github.com>2018-03-02 23:28:10 +0100
commitc9af23f6634031969e1e53bf293ef99895531c99 (patch)
treef93a2fa982a83cc9269a9a18e2b19555a0fca795
parent7cd3bb059f9eb729650614d00cabf65f857c7af9 (diff)
parent78fe9b8cc5658f99d38ee5812ced1d91c7639706 (diff)
downloadimag-c9af23f6634031969e1e53bf293ef99895531c99.zip
imag-c9af23f6634031969e1e53bf293ef99895531c99.tar.gz
Merge pull request #1314 from matthiasbeyer/imag/do-not-instantiate-runtime
Change "imag" impl to not instantiate Runtime object
-rw-r--r--bin/core/imag/Cargo.toml1
-rw-r--r--bin/core/imag/src/main.rs38
-rw-r--r--lib/core/libimagrt/src/runtime.rs3
3 files changed, 28 insertions, 14 deletions
diff --git a/bin/core/imag/Cargo.toml b/bin/core/imag/Cargo.toml
index fcdfb96..4c75e19 100644
--- a/bin/core/imag/Cargo.toml
+++ b/bin/core/imag/Cargo.toml
@@ -26,6 +26,7 @@ walkdir = "1"
log = "0.4.0"
toml = "0.4"
toml-query = "0.6"
+is-match = "0.1"
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
diff --git a/bin/core/imag/src/main.rs b/bin/core/imag/src/main.rs
index 2bbf101..f13ba6c 100644
--- a/bin/core/imag/src/main.rs
+++ b/bin/core/imag/src/main.rs
@@ -22,6 +22,7 @@ extern crate clap;
extern crate walkdir;
extern crate toml;
extern crate toml_query;
+#[macro_use] extern crate is_match;
#[macro_use] extern crate libimagrt;
extern crate libimagerror;
@@ -33,16 +34,19 @@ use std::process::Stdio;
use std::io::ErrorKind;
use std::io::{stdout, Stdout, Write};
use std::collections::BTreeMap;
+use std::path::PathBuf;
use walkdir::WalkDir;
use clap::{Arg, ArgMatches, AppSettings, SubCommand};
use toml::Value;
use toml_query::read::TomlValueReadExt;
+use libimagrt::error::RuntimeErrorKind;
use libimagrt::runtime::Runtime;
-use libimagerror::trace::trace_error;
+use libimagrt::spec::CliSpec;
use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap;
+use libimagerror::trace::trace_error;
/// Returns the helptext, putting the Strings in cmds as possible
/// subcommands into it
@@ -165,15 +169,23 @@ fn main() {
}
}
- let rt = Runtime::new(app)
- .unwrap_or_else(|e| {
- let _ = writeln!(out, "Runtime couldn't be setup. Exiting")
- .to_exit_code()
- .unwrap_or_exit();
- trace_error(&e);
- exit(1);
- });
- let matches = rt.cli();
+ let matches = app.matches();
+ let rtp = ::libimagrt::runtime::get_rtp_match(&matches);
+ let configpath = matches
+ .value_of(Runtime::arg_config_name())
+ .map_or_else(|| rtp.clone(), PathBuf::from);
+ debug!("Config path = {:?}", configpath);
+ let config = match ::libimagrt::configuration::fetch_config(&configpath) {
+ Ok(c) => Some(c),
+ 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);
@@ -214,7 +226,7 @@ fn main() {
exit(0);
}
- let aliases = match fetch_aliases(&rt) {
+ let aliases = match fetch_aliases(config.as_ref()) {
Ok(aliases) => aliases,
Err(e) => {
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> {
- let cfg = rt.config().ok_or_else(|| String::from("No configuration found"))?;
+fn fetch_aliases(config: Option<&Value>) -> Result<BTreeMap<String, String>, String> {
+ let cfg = config.ok_or_else(|| String::from("No configuration found"))?;
let value = cfg
.read("imag.aliases")
.map_err(|_| String::from("Reading from config failed"));
diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs
index a0378d2..e1f8fa8 100644
--- a/lib/core/libimagrt/src/runtime.rs
+++ b/lib/core/libimagrt/src/runtime.rs
@@ -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;
matches.value_of(Runtime::arg_runtimepath_name())