git.delta.rocks / jrsonnet / refs/commits / d13245aaf3fc

difftreelog

feat --tla-str-file

Lach2020-08-25parent: #cb35228.patch.diff
in: master

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
before · cmds/jrsonnet/src/main.rs
1use clap::Clap;2use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts};3use jrsonnet_evaluator::{error::Result, EvaluationState};4use std::{path::PathBuf, rc::Rc};56#[cfg(feature = "mimalloc")]7#[global_allocator]8static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;910#[derive(Clap)]11// #[clap(help_heading = "DEBUG")]12struct DebugOpts {13	/// Required OS stack size.14	/// This shouldn't be changed unless jrsonnet is failing with stack overflow error.15	#[clap(long, name = "size")]16	pub os_stack: Option<usize>,17}1819#[derive(Clap)]20struct Opts {21	#[clap(flatten)]22	input: InputOpts,23	#[clap(flatten)]24	general: GeneralOpts,25	#[clap(flatten)]26	manifest: ManifestOpts,27	#[clap(flatten)]28	debug: DebugOpts,29}3031fn main() {32	let opts: Opts = Opts::parse();33	if let Some(size) = opts.debug.os_stack {34		std::thread::Builder::new()35			.stack_size(size * 1024 * 1024)36			.spawn(|| main_catch(opts))37			.expect("new thread spawned")38			.join()39			.expect("thread finished successfully");40	} else {41		main_catch(opts)42	}43}4445fn main_catch(opts: Opts) {46	let state = EvaluationState::default();47	if let Err(e) = main_real(&state, opts) {48		println!("{}", state.stringify_err(&e));49	}50}5152fn main_real(state: &EvaluationState, opts: Opts) -> Result<()> {53	opts.general.configure(&state)?;54	opts.manifest.configure(&state)?;5556	let val = if opts.input.evaluate {57		state.evaluate_snippet_raw(58			Rc::new(PathBuf::from("args")),59			(&opts.input.input as &str).into(),60		)?61	} else {62		state.evaluate_file_raw(&PathBuf::from(opts.input.input))?63	};6465	let val = state.with_tla(val)?;6667	println!("{}", state.manifest(val)?);6869	Ok(())70}
modifiedcrates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/ext.rs
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -1,7 +1,7 @@
 use crate::ConfigureState;
 use clap::Clap;
 use jrsonnet_evaluator::{error::Result, EvaluationState};
-use std::str::FromStr;
+use std::{fs::read_to_string, str::FromStr};
 
 #[derive(Clone)]
 pub struct ExtStr {
@@ -28,6 +28,31 @@
 	}
 }
 
+#[derive(Clone)]
+pub struct ExtFile {
+	pub name: String,
+	pub value: String,
+}
+
+impl FromStr for ExtFile {
+	type Err = String;
+
+	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+		let out: Vec<&str> = s.split('=').collect();
+		if out.len() != 2 {
+			return Err("bad ext-file syntax".to_owned());
+		}
+		let file = read_to_string(&out[1]);
+		match file {
+			Ok(content) => Ok(Self {
+				name: out[0].into(),
+				value: content,
+			}),
+			Err(e) => Err(format!("{}", e)),
+		}
+	}
+}
+
 #[derive(Clap)]
 // #[clap(help_heading = "EXTERNAL VARIABLES")]
 pub struct ExtVarOpts {
@@ -38,23 +63,33 @@
 	/// Can be accessed from code via `std.extVar("name")`.
 	#[clap(long, short = 'V', name = "name[=var data]", number_of_values = 1)]
 	ext_str: Vec<ExtStr>,
-	// / Read string external variable from file.
-	// / See also `--ext-str`
-	// #[clap(long, name = "name[=var path]", number_of_values = 1)]
-	// ext_str_file: Vec<ExtStr>,
+	/// Read string external variable from file.
+	/// See also `--ext-str`
+	#[clap(long, name = "name=var path", number_of_values = 1)]
+	ext_str_file: Vec<ExtFile>,
 	/// Add external variable from code.
 	/// See also `--ext-str`
 	#[clap(long, name = "name[=var source]", number_of_values = 1)]
 	ext_code: Vec<ExtStr>,
+	/// Read string external variable from file.
+	/// See also `--ext-str`
+	#[clap(long, name = "name=var code path", number_of_values = 1)]
+	ext_code_file: Vec<ExtFile>,
 }
 impl ConfigureState for ExtVarOpts {
 	fn configure(&self, state: &EvaluationState) -> Result<()> {
 		for ext in self.ext_str.iter() {
 			state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
 		}
+		for ext in self.ext_str_file.iter() {
+			state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+		}
 		for ext in self.ext_code.iter() {
 			state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;
 		}
+		for ext in self.ext_code_file.iter() {
+			state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;
+		}
 		Ok(())
 	}
 }
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -24,7 +24,7 @@
 		short = 'e',
 		about = "Treat input as code, evaluate them instead of reading file"
 	)]
-	pub evaluate: bool,
+	pub exec: bool,
 
 	#[clap(
 		about = "Path to the file to be compiled if `--evaluate` is unset, otherwise code itself"
modifiedcrates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,4 +1,4 @@
-use crate::{ConfigureState, ExtStr};
+use crate::{ConfigureState, ExtFile, ExtStr};
 use clap::Clap;
 use jrsonnet_evaluator::{error::Result, EvaluationState};
 
@@ -13,21 +13,31 @@
 	tla_str: Vec<ExtStr>,
 	/// Read top level argument string from file.
 	/// See also `--tla-str`
-	// #[clap(long, name = "name[=tla path]", number_of_values = 1)]
-	// tla_str_file: Vec<ExtStr>,
+	#[clap(long, name = "name=tla path", number_of_values = 1)]
+	tla_str_file: Vec<ExtFile>,
 	/// Add top level argument from code.
 	/// See also `--tla-str`
 	#[clap(long, name = "name[=tla source]", number_of_values = 1)]
 	tla_code: Vec<ExtStr>,
+	/// Read top level argument code from file.
+	/// See also `--tla-str`
+	#[clap(long, name = "name=tla code path", number_of_values = 1)]
+	tla_code_file: Vec<ExtFile>,
 }
 impl ConfigureState for TLAOpts {
 	fn configure(&self, state: &EvaluationState) -> Result<()> {
 		for tla in self.tla_str.iter() {
 			state.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into());
 		}
+		for tla in self.tla_str_file.iter() {
+			state.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into())
+		}
 		for tla in self.tla_code.iter() {
 			state.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;
 		}
+		for tla in self.tla_code_file.iter() {
+			state.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;
+		}
 		Ok(())
 	}
 }