Skip to content

Commit

Permalink
fix: use correct keys when importing TLAs
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft committed Nov 2, 2023
1 parent 2ceb920 commit e83a200
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmds/jrsonnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use jrsonnet_cli::{GcOpts, ManifestOpts, MiscOpts, OutputOpts, StdOpts, TlaOpts,
use jrsonnet_evaluator::{
apply_tla, bail,
error::{Error as JrError, ErrorKind},
ImportResolver, ResultExt, State, Val,
ResultExt, State, Val,
};

#[cfg(feature = "mimalloc")]
Expand Down
39 changes: 31 additions & 8 deletions crates/jrsonnet-cli/src/tla.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::path::PathBuf;
use std::{
ffi::{OsStr, OsString},
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
};

use clap::Parser;
use jrsonnet_evaluator::{
bail,
error::{ErrorKind, Result},
function::TlaArg,
gc::GcHashMap,
Expand All @@ -25,25 +30,26 @@ pub struct TlaOpts {
/// 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<PathBuf>,
tla_str_file: Vec<OsString>,
/// 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<PathBuf>,
tla_code_file: Vec<OsString>,
}
impl TlaOpts {
pub fn into_args_in(self, state: &State) -> Result<GcHashMap<IStr, TlaArg>> {
let mut out = GcHashMap::new();
for (name, value) in self.tla_str.iter().map(|c| (&c.name, &c.value)) {
out.insert(name.into(), TlaArg::String(value.into()));
}
for path in self.tla_str_file {
for file in self.tla_str_file {
let (key, path) = dbg!(parse_named_tla_path(&file))?;
out.insert(
path.to_string_lossy().into(),
key.into(),
TlaArg::Lazy(Thunk::new(ImportStrThunk {
state: state.clone(),
path,
Expand All @@ -53,7 +59,7 @@ impl TlaOpts {
for (name, code) in self.tla_code.iter().map(|c| (&c.name, &c.value)) {
let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
out.insert(
name.as_str().into(),
name.into(),
TlaArg::Code(
jrsonnet_parser::parse(
code,
Expand All @@ -68,9 +74,10 @@ impl TlaOpts {
),
);
}
for path in self.tla_code_file {
for file in self.tla_code_file {
let (key, path) = dbg!(parse_named_tla_path(&file))?;
out.insert(
path.to_string_lossy().into(),
key.into(),
TlaArg::Lazy(Thunk::new(ImportCodeThunk {
state: state.clone(),
path,
Expand All @@ -81,6 +88,22 @@ impl TlaOpts {
}
}

fn parse_named_tla_path(raw: &OsString) -> Result<(&str, PathBuf)> {
let mut parts = raw.as_bytes().splitn(2, |&byte| byte == b'=');
let Some(key) = parts.next() else {
bail!("No TLA key was specified");
};

let Ok(key) = std::str::from_utf8(key) else {
bail!("Invalid TLA map");
};
Ok(if let Some(value) = parts.next() {
(key, Path::new(OsStr::from_bytes(value)).to_owned())
} else {
(key, std::env::var_os(key).unwrap_or_default().into())
})
}

#[derive(Trace)]
struct ImportStrThunk {
path: PathBuf,
Expand Down

0 comments on commit e83a200

Please sign in to comment.