Skip to content

Commit

Permalink
treefile: Support variable substitution in metadata
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jlebon committed Nov 16, 2024
1 parent 16399a7 commit 8dc8f84
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/treefile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions rust/src/treefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -3794,6 +3801,7 @@ conditional-include:
nested: table
metadata:
foo: baz
bar: substituted ${mystr_override}
deeper:
answer: 9001
etc-group-members:
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8dc8f84

Please sign in to comment.