difftreelog
feat add -c, -o, -m flags
in: master
3 files changed
Cargo.lockdiffbeforeafterboth148 "jrsonnet-evaluator",148 "jrsonnet-evaluator",149 "jrsonnet-parser",149 "jrsonnet-parser",150 "mimallocator",150 "mimallocator",151 "thiserror",151]152]152153153[[package]]154[[package]]176 "serde",177 "serde",177 "serde_json",178 "serde_json",178 "structdump",179 "structdump",180 "thiserror",179]181]180182181[[package]]183[[package]]428 "unicode-width",430 "unicode-width",429]431]432433[[package]]434name = "thiserror"435version = "1.0.20"436source = "registry+https://github.com/rust-lang/crates.io-index"437checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08"438dependencies = [439 "thiserror-impl",440]441442[[package]]443name = "thiserror-impl"444version = "1.0.20"445source = "registry+https://github.com/rust-lang/crates.io-index"446checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793"447dependencies = [448 "proc-macro2",449 "quote",450 "syn",451]430452431[[package]]453[[package]]432name = "unescape"454name = "unescape"cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -19,6 +19,7 @@
jrsonnet-cli = { path = "../../crates/jrsonnet-cli", version = "0.1.0" }
# TODO: Fix mimalloc compile errors, and use them
mimallocator = { version = "0.1.3", optional = true }
+thiserror = "1.0.20"
[dependencies.clap]
git = "https://github.com/clap-rs/clap"
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,7 +1,12 @@
use clap::Clap;
-use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts};
-use jrsonnet_evaluator::{error::Result, EvaluationState};
-use std::{path::PathBuf, rc::Rc};
+use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
+use jrsonnet_evaluator::{error::LocError, EvaluationState};
+use std::{
+ fs::{create_dir_all, File},
+ io::Write,
+ path::PathBuf,
+ rc::Rc,
+};
#[cfg(feature = "mimalloc")]
#[global_allocator]
@@ -25,6 +30,8 @@
#[clap(flatten)]
manifest: ManifestOpts,
#[clap(flatten)]
+ output: OutputOpts,
+ #[clap(flatten)]
debug: DebugOpts,
}
@@ -42,14 +49,32 @@
}
}
+#[derive(thiserror::Error, Debug)]
+enum Error {
+ // Handled differently
+ #[error("evaluation error")]
+ Evaluation(jrsonnet_evaluator::error::LocError),
+ #[error("io error")]
+ Io(#[from] std::io::Error),
+}
+impl From<LocError> for Error {
+ fn from(e: LocError) -> Self {
+ Self::Evaluation(e)
+ }
+}
+
fn main_catch(opts: Opts) {
let state = EvaluationState::default();
if let Err(e) = main_real(&state, opts) {
- println!("{}", state.stringify_err(&e));
+ if let Error::Evaluation(e) = e {
+ println!("{}", state.stringify_err(&e));
+ } else {
+ println!("{}", e);
+ }
}
}
-fn main_real(state: &EvaluationState, opts: Opts) -> Result<()> {
+fn main_real(state: &EvaluationState, opts: Opts) -> Result<(), Error> {
opts.general.configure(&state)?;
opts.manifest.configure(&state)?;
@@ -64,7 +89,34 @@
let val = state.with_tla(val)?;
- println!("{}", state.manifest(val)?);
+ if let Some(multi) = opts.output.multi {
+ if opts.output.create_output_dirs {
+ let mut dir = multi.clone();
+ dir.pop();
+ create_dir_all(dir)?;
+ }
+ for (file, data) in state.manifest_multi(val)?.iter() {
+ let mut path = multi.clone();
+ path.push(&file as &str);
+ if opts.output.create_output_dirs {
+ let mut dir = path.clone();
+ dir.pop();
+ create_dir_all(dir)?;
+ }
+ let mut file = File::create(path)?;
+ write!(file, "{}", data)?;
+ }
+ } else if let Some(path) = opts.output.output_file {
+ if opts.output.create_output_dirs {
+ let mut dir = path.clone();
+ dir.pop();
+ create_dir_all(dir)?;
+ }
+ let mut file = File::create(path)?;
+ write!(file, "{}", state.manifest(val)?)?;
+ } else {
+ println!("{}", state.manifest(val)?);
+ }
Ok(())
}