From 8dc8f8464df22ffb35caf9c3e30982d0ebffd30f Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Sat, 16 Nov 2024 08:47:30 -0500 Subject: [PATCH] treefile: Support variable substitution in `metadata` I want to stop abusing the `rojig` key in the CoreOS treefiles. But before we can switch over to `metadata`, we need variable substitution supported there too. Note the `metadata` key can have arbitrarily nested values. For now, we just implement variable substitution for the first level of values since that's all we need. --- docs/treefile.md | 1 + rust/src/treefile.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/docs/treefile.md b/docs/treefile.md index 1ab76ef37e..6fb1499245 100644 --- a/docs/treefile.md +++ b/docs/treefile.md @@ -26,6 +26,7 @@ It supports the following parameters: * `metadata`: Mapping of strings to values, optional. This can be used for other tools to insert arbitrary metadata into the treefile which they parse later, for example via `rpm-ostree compose tree --print-metadata-json`. + All (top-level) objects of type string support variable substitution. * `gpg-key` (or `gpg_key`): string, optional: Key ID for GPG signing; the secret key must be in the home directory of the building user. Defaults to diff --git a/rust/src/treefile.rs b/rust/src/treefile.rs index 5a15409646..ba18601474 100644 --- a/rust/src/treefile.rs +++ b/rust/src/treefile.rs @@ -2855,6 +2855,13 @@ impl TreeComposeConfig { substitute_string(&substvars, &mut rojig.summary)?; substitute_string_option(&substvars, &mut rojig.description)?; } + if let Some(ref mut metadata) = self.base.metadata { + for val in metadata.values_mut() { + if let serde_json::Value::String(ref mut s) = val { + substitute_string(&substvars, s)?; + } + } + } if let Some(ref mut add_commit_metadata) = self.base.add_commit_metadata { for val in add_commit_metadata.values_mut() { if let serde_json::Value::String(ref mut s) = val { @@ -3794,6 +3801,7 @@ conditional-include: nested: table metadata: foo: baz + bar: substituted ${mystr_override} deeper: answer: 9001 etc-group-members: @@ -3836,6 +3844,10 @@ conditional-include: ); let data = tf.base.metadata.as_ref().unwrap(); assert_eq!(data.get("foo").unwrap().as_str().unwrap(), "baz"); + assert_eq!( + data.get("bar").unwrap().as_str().unwrap(), + "substituted overridden" + ); assert_eq!(data.get("name").unwrap().as_str().unwrap(), "fedora-coreos"); let deeper = data.get("deeper").unwrap().as_object().unwrap(); assert_eq!(deeper.get("answer").unwrap().as_i64().unwrap(), 9001);