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
53 opts.general.configure(&state)?;53 opts.general.configure(&state)?;
54 opts.manifest.configure(&state)?;54 opts.manifest.configure(&state)?;
5555
56 let val = if opts.input.evaluate {56 let val = if opts.input.exec {
57 state.evaluate_snippet_raw(57 state.evaluate_snippet_raw(
58 Rc::new(PathBuf::from("args")),58 Rc::new(PathBuf::from("args")),
59 (&opts.input.input as &str).into(),59 (&opts.input.input as &str).into(),
modifiedcrates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth
1use crate::ConfigureState;1use crate::ConfigureState;
2use clap::Clap;2use clap::Clap;
3use jrsonnet_evaluator::{error::Result, EvaluationState};3use jrsonnet_evaluator::{error::Result, EvaluationState};
4use std::str::FromStr;4use std::{fs::read_to_string, str::FromStr};
55
6#[derive(Clone)]6#[derive(Clone)]
7pub struct ExtStr {7pub struct ExtStr {
28 }28 }
29}29}
30
31#[derive(Clone)]
32pub struct ExtFile {
33 pub name: String,
34 pub value: String,
35}
36
37impl FromStr for ExtFile {
38 type Err = String;
39
40 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
41 let out: Vec<&str> = s.split('=').collect();
42 if out.len() != 2 {
43 return Err("bad ext-file syntax".to_owned());
44 }
45 let file = read_to_string(&out[1]);
46 match file {
47 Ok(content) => Ok(Self {
48 name: out[0].into(),
49 value: content,
50 }),
51 Err(e) => Err(format!("{}", e)),
52 }
53 }
54}
3055
31#[derive(Clap)]56#[derive(Clap)]
32// #[clap(help_heading = "EXTERNAL VARIABLES")]57// #[clap(help_heading = "EXTERNAL VARIABLES")]
38 /// Can be accessed from code via `std.extVar("name")`.63 /// Can be accessed from code via `std.extVar("name")`.
39 #[clap(long, short = 'V', name = "name[=var data]", number_of_values = 1)]64 #[clap(long, short = 'V', name = "name[=var data]", number_of_values = 1)]
40 ext_str: Vec<ExtStr>,65 ext_str: Vec<ExtStr>,
41 // / Read string external variable from file.66 /// Read string external variable from file.
42 // / See also `--ext-str`67 /// See also `--ext-str`
43 // #[clap(long, name = "name[=var path]", number_of_values = 1)]68 #[clap(long, name = "name=var path", number_of_values = 1)]
44 // ext_str_file: Vec<ExtStr>,69 ext_str_file: Vec<ExtFile>,
45 /// Add external variable from code.70 /// Add external variable from code.
46 /// See also `--ext-str`71 /// See also `--ext-str`
47 #[clap(long, name = "name[=var source]", number_of_values = 1)]72 #[clap(long, name = "name[=var source]", number_of_values = 1)]
48 ext_code: Vec<ExtStr>,73 ext_code: Vec<ExtStr>,
74 /// Read string external variable from file.
75 /// See also `--ext-str`
76 #[clap(long, name = "name=var code path", number_of_values = 1)]
77 ext_code_file: Vec<ExtFile>,
49}78}
50impl ConfigureState for ExtVarOpts {79impl ConfigureState for ExtVarOpts {
51 fn configure(&self, state: &EvaluationState) -> Result<()> {80 fn configure(&self, state: &EvaluationState) -> Result<()> {
52 for ext in self.ext_str.iter() {81 for ext in self.ext_str.iter() {
53 state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());82 state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
54 }83 }
84 for ext in self.ext_str_file.iter() {
85 state.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
86 }
55 for ext in self.ext_code.iter() {87 for ext in self.ext_code.iter() {
56 state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;88 state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;
57 }89 }
90 for ext in self.ext_code_file.iter() {
91 state.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;
92 }
58 Ok(())93 Ok(())
59 }94 }
60}95}
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
24 short = 'e',24 short = 'e',
25 about = "Treat input as code, evaluate them instead of reading file"25 about = "Treat input as code, evaluate them instead of reading file"
26 )]26 )]
27 pub evaluate: bool,27 pub exec: bool,
2828
29 #[clap(29 #[clap(
30 about = "Path to the file to be compiled if `--evaluate` is unset, otherwise code itself"30 about = "Path to the file to be compiled if `--evaluate` is unset, otherwise code itself"
modifiedcrates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth
1use crate::{ConfigureState, ExtStr};1use crate::{ConfigureState, ExtFile, ExtStr};
2use clap::Clap;2use clap::Clap;
3use jrsonnet_evaluator::{error::Result, EvaluationState};3use jrsonnet_evaluator::{error::Result, EvaluationState};
44
13 tla_str: Vec<ExtStr>,13 tla_str: Vec<ExtStr>,
14 /// Read top level argument string from file.14 /// Read top level argument string from file.
15 /// See also `--tla-str`15 /// See also `--tla-str`
16 // #[clap(long, name = "name[=tla path]", number_of_values = 1)]16 #[clap(long, name = "name=tla path", number_of_values = 1)]
17 // tla_str_file: Vec<ExtStr>,17 tla_str_file: Vec<ExtFile>,
18 /// Add top level argument from code.18 /// Add top level argument from code.
19 /// See also `--tla-str`19 /// See also `--tla-str`
20 #[clap(long, name = "name[=tla source]", number_of_values = 1)]20 #[clap(long, name = "name[=tla source]", number_of_values = 1)]
21 tla_code: Vec<ExtStr>,21 tla_code: Vec<ExtStr>,
22 /// Read top level argument code from file.
23 /// See also `--tla-str`
24 #[clap(long, name = "name=tla code path", number_of_values = 1)]
25 tla_code_file: Vec<ExtFile>,
22}26}
23impl ConfigureState for TLAOpts {27impl ConfigureState for TLAOpts {
24 fn configure(&self, state: &EvaluationState) -> Result<()> {28 fn configure(&self, state: &EvaluationState) -> Result<()> {
25 for tla in self.tla_str.iter() {29 for tla in self.tla_str.iter() {
26 state.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into());30 state.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into());
27 }31 }
32 for tla in self.tla_str_file.iter() {
33 state.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into())
34 }
28 for tla in self.tla_code.iter() {35 for tla in self.tla_code.iter() {
29 state.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;36 state.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;
30 }37 }
38 for tla in self.tla_code_file.iter() {
39 state.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;
40 }
31 Ok(())41 Ok(())
32 }42 }
33}43}