use std::path::Path;
use crate::Result;
use crate::error::Error;
use crate::formats::ToolCx;
use crate::host_env::TF_VAR_PREFIX;
use crate::project::variables::InputVariable;
pub(super) fn refuse_unset(cx: &ToolCx<'_>) -> Result<()> {
if cx.host_env.sets_terraform_args() {
return Ok(());
}
let Ok(variables) = cx.project.variables() else { return Ok(()) };
let given = |var: &str| cx.env.iter().any(|(k, _)| k == var) || cx.host_env.sets_terraform(var);
unset(&variables, given, &cx.project.dir).map_or(Ok(()), Err)
}
fn unset(variables: &[InputVariable], given: impl Fn(&str) -> bool, dir: &Path) -> Option<Error> {
let mut from_files: Option<Option<Vec<String>>> = None;
for v in variables.iter().filter(|v| v.default.is_none()) {
if given(&format!("{TF_VAR_PREFIX}{}", v.name)) {
continue;
}
match from_files.get_or_insert_with(|| names_set(dir)) {
Some(set) if !set.contains(&v.name) => {}
_ => continue,
}
return Some(if v.sensitive {
Error::SecretMissing { name: v.name.clone() }
} else {
Error::Input {
name: v.name.clone(),
reason: "the module requires it (no default) and nothing gives it a value".to_owned(),
}
});
}
None
}
fn names_set(dir: &Path) -> Option<Vec<String>> {
let mut names = Vec::new();
let Ok(entries) = std::fs::read_dir(dir) else { return None };
for entry in entries {
let path = entry.ok()?.path();
let Some(file) = path.file_name().and_then(|n| n.to_str()) else { continue };
let json = file == "terraform.tfvars.json" || file.ends_with(".auto.tfvars.json");
let hcl = file == "terraform.tfvars" || file.ends_with(".auto.tfvars");
if !json && !hcl {
continue;
}
let text = std::fs::read_to_string(&path).ok()?;
names.extend(if json { json_names(&text)? } else { hcl_names(&text) });
}
Some(names)
}
fn json_names(text: &str) -> Option<Vec<String>> {
let doc: serde_json::Value = serde_json::from_str(text).ok()?;
Some(doc.as_object()?.keys().cloned().collect())
}
fn hcl_names(text: &str) -> Vec<String> {
text.lines()
.filter_map(|line| line.split_once('='))
.map(|(name, _)| name.trim().trim_matches('"').to_owned())
.filter(|name| !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn module(files: &[(&str, &str)]) -> (tempfile::TempDir, Vec<InputVariable>) {
let dir = tempfile::tempdir().expect("a module");
let main = "variable \"token\" {\n sensitive = true\n}\nvariable \"region\" {}\nvariable \"size\" {\n default = 1\n}\n";
std::fs::write(dir.path().join("main.tf"), main).expect("main.tf");
for (name, text) in files {
std::fs::write(dir.path().join(name), text).expect("a variable file");
}
let variables = crate::variables_of(dir.path()).expect("its variables");
(dir, variables)
}
fn refused(variables: &[InputVariable], given: &[&str], dir: &Path) -> Option<(crate::ErrorCode, String)> {
unset(variables, |var| given.contains(&var), dir).map(|e| {
let name = match &e {
Error::SecretMissing { name } | Error::Input { name, .. } => name.clone(),
other => format!("{other:?}"),
};
(e.code(), name)
})
}
#[test]
fn a_required_variable_with_no_value_from_any_place_is_refused_by_its_kind() {
let (dir, vars) = module(&[]);
assert_eq!(refused(&vars, &[], dir.path()), Some((crate::ErrorCode::InputInvalid, "region".to_owned())));
let region = ["TF_VAR_region"];
assert_eq!(refused(&vars, ®ion, dir.path()), Some((crate::ErrorCode::SecretMissing, "token".to_owned())));
assert_eq!(refused(&vars, &["TF_VAR_token", "TF_VAR_region"], dir.path()), None, "`size` has its default");
}
#[test]
fn a_value_from_a_variable_file_or_one_that_cant_be_read_is_never_refused() {
for files in [
&[("terraform.tfvars", "token = \"x\"\nregion = \"eu\"\n")][..],
&[("terraform.tfvars.json", r#"{"token": "x"}"#), ("stack.auto.tfvars", "region = \"eu\"\n")][..],
&[("prod.auto.tfvars.json", r#"{"token": "x", "region": "eu"}"#)][..],
&[("terraform.tfvars.json", "{ not json")][..],
] {
let (dir, vars) = module(files);
assert_eq!(refused(&vars, &[], dir.path()), None, "{files:?}");
}
let (dir, vars) = module(&[("other.tfvars", "token = \"x\"\nregion = \"eu\"\n")]);
assert_eq!(
refused(&vars, &[], dir.path()),
Some((crate::ErrorCode::InputInvalid, "region".to_owned())),
"a file Terraform doesn't read by itself gives nothing"
);
}
#[test]
fn a_tfvars_files_names_are_read_and_a_json_one_that_doesnt_parse_reads_as_none() {
assert_eq!(
hcl_names("region = \"eu\"\n# token = x\n \"size\" = 3\ntags = {\n owner = \"me\"\n}\n"),
["region", "size", "tags", "owner"]
);
let mut names = json_names(r#"{"token": "x", "region": "eu"}"#).expect("an object");
names.sort();
assert_eq!(names, ["region", "token"]);
assert_eq!((json_names("[1, 2]"), json_names("not json")), (None, None));
}
}