difftreelog
refactor extract shared CLI code to jrsonnet-cli
in: master
8 files changed
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -11,9 +11,10 @@
[dependencies]
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "1.0.0" }
jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "1.0.0" }
-jrsonnet-trace = { path = "../../crates/jrsonnet-trace", version = "1.0.0" }
+jrsonnet-cli = { path = "../../crates/jrsonnet-cli", version = "0.1.0" }
# TODO: Fix mimalloc compile errors, and use them
mimallocator = "0.1.3"
[dependencies.clap]
-version = "3.0.0-beta.1"
+git = "https://github.com/clap-rs/clap"
+rev = "48d308a8ab9e96d4b21336e428feee420dbac51d"
cmds/jrsonnet/src/main.rsdiffbeforeafterboth1use clap::Clap;2use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts};3use jrsonnet_evaluator::{EvaluationState, Result};4use std::{path::PathBuf, rc::Rc};56#[global_allocator]7static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;89#[derive(Clap)]10// #[clap(help_heading = "DEBUG")]11struct DebugOpts {12 /// Required OS stack size, probally you shouldn't change it, unless jrsonnet is failing with stack overflow13 #[clap(long, name = "size")]14 pub os_stack: Option<usize>,15}1617#[derive(Clap)]18struct Opts {19 #[clap(flatten)]20 input: InputOpts,21 #[clap(flatten)]22 general: GeneralOpts,23 #[clap(flatten)]24 manifest: ManifestOpts,25 #[clap(flatten)]26 debug: DebugOpts,27}2829fn main() {30 let opts: Opts = Opts::parse();31 if let Some(size) = opts.debug.os_stack {32 std::thread::Builder::new()33 .stack_size(size * 1024 * 1024)34 .spawn(|| main_catch(opts))35 .expect("new thread spawned")36 .join()37 .expect("thread finished successfully");38 } else {39 main_catch(opts)40 }41}4243fn main_catch(opts: Opts) {44 let state = EvaluationState::default();45 if let Err(e) = main_real(&state, opts) {46 println!("{}", state.stringify_err(&e));47 }48}4950fn main_real(state: &EvaluationState, opts: Opts) -> Result<()> {51 opts.general.configure(&state)?;52 opts.manifest.configure(&state)?;5354 let val = if opts.input.evaluate {55 state.evaluate_snippet_raw(56 Rc::new(PathBuf::from("args")),57 (&opts.input.input as &str).into(),58 )?59 } else {60 state.evaluate_file_raw(&PathBuf::from(opts.input.input))?61 };6263 let val = state.with_tla(val)?;6465 println!("{}", state.manifest(val)?);6667 Ok(())68}crates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "jrsonnet-cli"
+description = "Utilities for building jrsonnet CLIs"
+version = "0.1.0"
+authors = ["Лач <iam@lach.pw>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "1.0.0", features = ["explaining-traces"] }
+jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "1.0.0" }
+
+[dependencies.clap]
+git = "https://github.com/clap-rs/clap"
+rev = "48d308a8ab9e96d4b21336e428feee420dbac51d"
crates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -0,0 +1,59 @@
+use crate::ConfigureState;
+use clap::Clap;
+use jrsonnet_evaluator::{EvaluationState, Result};
+use std::str::FromStr;
+
+#[derive(Clone)]
+pub struct ExtStr {
+ pub name: String,
+ pub value: String,
+}
+
+impl FromStr for ExtStr {
+ type Err = &'static str;
+ fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+ let out: Vec<_> = s.split('=').collect();
+ match out.len() {
+ 1 => Ok(ExtStr {
+ name: out[0].to_owned(),
+ value: std::env::var(out[0]).or(Err("missing env var"))?,
+ }),
+ 2 => Ok(ExtStr {
+ name: out[0].to_owned(),
+ value: out[1].to_owned(),
+ }),
+
+ _ => Err("bad ext-str syntax"),
+ }
+ }
+}
+
+#[derive(Clap)]
+// #[clap(help_heading = "EXTERNAL VARIABLES")]
+pub struct ExtVarOpts {
+ /// Add string external variable.
+ /// External variables are globally available, so prefer to use top level arguments where possible.
+ /// If [=data] is not set, then it will be read from `name` env variable.
+ /// 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>,
+ /// Add external variable from code.
+ /// See also `--ext-str`
+ #[clap(long, name = "name[=var source]", number_of_values = 1)]
+ ext_code: Vec<ExtStr>,
+}
+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_code.iter() {
+ state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;
+ }
+ Ok(())
+ }
+}
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -0,0 +1,90 @@
+mod ext;
+mod manifest;
+mod tla;
+mod trace;
+
+pub use ext::*;
+pub use manifest::*;
+pub use tla::*;
+pub use trace::*;
+
+use clap::Clap;
+use jrsonnet_evaluator::{EvaluationState, FileImportResolver, Result};
+use std::path::PathBuf;
+
+pub trait ConfigureState {
+ fn configure(&self, state: &EvaluationState) -> Result<()>;
+}
+
+#[derive(Clap)]
+// #[clap(help_heading = "INPUT")]
+pub struct InputOpts {
+ #[clap(
+ long,
+ short = 'e',
+ about = "Threat input as code, evaluate them instead of reading file"
+ )]
+ pub evaluate: bool,
+
+ #[clap(about = "File to compile (Or code directly, if --evaluate is specified)")]
+ pub input: String,
+}
+
+#[derive(Clap)]
+// #[clap(help_heading = "OPTIONS")]
+pub struct MiscOpts {
+ /// Disable standard library. By default, standard library will be available via global `std` variable.
+ /// Beware that standard library will still be loaded if choosen manifestification method is not `none`
+ #[clap(long)]
+ no_stdlib: bool,
+
+ /// Number of allowed stack frames, stack overflow error will be returned if reached
+ #[clap(long, short = 's', default_value = "200")]
+ max_stack: usize,
+
+ /// Library search dirs. Any not found `imported` file will be searched in them.
+ /// Can also be specified via JSONNET_PATH, which should contain colon (semicolon on Windows) delimited list of directories
+ #[clap(long, short = 'J')]
+ jpath: Vec<PathBuf>,
+}
+impl ConfigureState for MiscOpts {
+ fn configure(&self, state: &EvaluationState) -> Result<()> {
+ if !self.no_stdlib {
+ state.with_stdlib();
+ }
+
+ state.set_import_resolver(Box::new(FileImportResolver {
+ library_paths: self.jpath.clone(),
+ }));
+
+ state.set_max_stack(self.max_stack);
+ Ok(())
+ }
+}
+
+/// For general configuration of jsonnet
+#[derive(Clap)]
+#[clap(name = "jrsonnet", version, author)]
+pub struct GeneralOpts {
+ #[clap(flatten)]
+ misc: MiscOpts,
+
+ #[clap(flatten)]
+ tla: TLAOpts,
+ #[clap(flatten)]
+ ext: ExtVarOpts,
+
+ #[clap(flatten)]
+ trace: TraceOpts,
+}
+
+impl ConfigureState for GeneralOpts {
+ fn configure(&self, state: &EvaluationState) -> Result<()> {
+ // Configure trace first, because tla-code/ext-code can throw
+ self.trace.configure(state)?;
+ self.misc.configure(state)?;
+ self.tla.configure(state)?;
+ self.ext.configure(state)?;
+ Ok(())
+ }
+}
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -0,0 +1,59 @@
+use crate::ConfigureState;
+use clap::Clap;
+use jrsonnet_evaluator::{EvaluationState, ManifestFormat, Result};
+use std::str::FromStr;
+
+pub enum ManifestFormatName {
+ /// Expect string as output, and write them directly
+ None,
+ Json,
+ Yaml,
+}
+
+impl FromStr for ManifestFormatName {
+ type Err = &'static str;
+ fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+ Ok(match s {
+ "none" => ManifestFormatName::None,
+ "json" => ManifestFormatName::Json,
+ "yaml" => ManifestFormatName::Yaml,
+ _ => return Err("no such format"),
+ })
+ }
+}
+
+#[derive(Clap)]
+// #[clap(group = clap::ArgGroup::new("output_format"), help_heading = "MANIFESTIFICATION OUTPUT")]
+pub struct ManifestOpts {
+ /// Output format, wraps resulting value to corresponding std.manifest call.
+ /// If none - then jsonnet file is expected to return plain string value, otherwise
+ /// output will be serialized to specified format
+ #[clap(long, short = 'f', default_value = "json", possible_values = &["none", "json", "yaml"], group = "output_format")]
+ format: ManifestFormatName,
+ /// Expect string as output, and write them directly.
+ /// Shortcut for --format=none, and can't be set with format together
+ #[clap(long, short = 'S', group = "output_format")]
+ string: bool,
+ /// Numbed of spaces to pad output manifest with.
+ /// 0 for hard tabs, -1 for single line output
+ #[clap(long, default_value = "4")]
+ line_padding: usize,
+}
+impl ConfigureState for ManifestOpts {
+ fn configure(&self, state: &EvaluationState) -> Result<()> {
+ if self.string {
+ state.set_manifest_format(ManifestFormat::None);
+ } else {
+ match self.format {
+ ManifestFormatName::None => state.set_manifest_format(ManifestFormat::None),
+ ManifestFormatName::Json => {
+ state.set_manifest_format(ManifestFormat::Json(self.line_padding))
+ }
+ ManifestFormatName::Yaml => {
+ state.set_manifest_format(ManifestFormat::Yaml(self.line_padding))
+ }
+ }
+ }
+ Ok(())
+ }
+}
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -0,0 +1,33 @@
+use crate::{ConfigureState, ExtStr};
+use clap::Clap;
+use jrsonnet_evaluator::{EvaluationState, Result};
+
+#[derive(Clap)]
+// #[clap(help_heading = "TOP LEVEL ARGUMENTS")]
+pub struct TLAOpts {
+ /// Add top level string argument.
+ /// Top level arguments will be passed to function before manifestification stage.
+ /// prefer using them instead of ExtVars.
+ /// If [=data] is not set, then it will be read from `name` env variable.
+ #[clap(long, short = 'A', name = "name[=tla data]", number_of_values = 1)]
+ 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>,
+ /// Add top level argument from code.
+ /// See also `--tla-str`
+ #[clap(long, name = "name[=tla source]", number_of_values = 1)]
+ tla_code: Vec<ExtStr>,
+}
+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_code.iter() {
+ state.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;
+ }
+ Ok(())
+ }
+}
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -0,0 +1,53 @@
+use crate::ConfigureState;
+use clap::Clap;
+use jrsonnet_evaluator::{
+ trace::{CompactFormat, ExplainingFormat, PathResolver},
+ EvaluationState, Result,
+};
+use std::str::FromStr;
+
+#[derive(PartialEq)]
+pub enum TraceFormatName {
+ Compact,
+ Explaining,
+}
+
+impl FromStr for TraceFormatName {
+ type Err = &'static str;
+ fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+ Ok(match s {
+ "compact" => TraceFormatName::Compact,
+ "explaining" => TraceFormatName::Explaining,
+ _ => return Err("no such format"),
+ })
+ }
+}
+
+#[derive(Clap)]
+// #[clap(help_heading = "STACK TRACE VISUAL")]
+pub struct TraceOpts {
+ /// How stack traces should be displayed in console.
+ /// compact format only shows `filename:line:column`s, where explaining displays source code
+ /// with attached trace annotations, so it is more verbose
+ #[clap(long, possible_values = &["compact", "explaining"])]
+ trace_format: Option<TraceFormatName>,
+ /// Amount of stack trace elements to be displayed, if zero - then full stack trace will be displayed
+ #[clap(long, short = 't', default_value = "20")]
+ max_trace: usize,
+}
+impl ConfigureState for TraceOpts {
+ fn configure(&self, state: &EvaluationState) -> Result<()> {
+ let resolver = PathResolver::Absolute;
+ match self.trace_format.as_ref().unwrap_or(&TraceFormatName::Compact) {
+ TraceFormatName::Compact => state.set_trace_format(Box::new(CompactFormat {
+ resolver,
+ padding: 4,
+ })),
+ TraceFormatName::Explaining => {
+ state.set_trace_format(Box::new(ExplainingFormat { resolver }))
+ }
+ }
+ state.set_max_trace(self.max_trace);
+ Ok(())
+ }
+}