From 8f77970683068fe62f7c14654566fa923036e6ba Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 5 Mar 2021 18:22:48 +0000 Subject: [PATCH] client: Add API to fetch base commit metadata Also desired by Zincati. --- rust/rpmostree-client/src/lib.rs | 19 +++++++++++++++++++ rust/rpmostree-client/tests/parse.rs | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/rust/rpmostree-client/src/lib.rs b/rust/rpmostree-client/src/lib.rs index daf7250b..9a20e9cf 100644 --- a/rust/rpmostree-client/src/lib.rs +++ b/rust/rpmostree-client/src/lib.rs @@ -76,6 +76,25 @@ impl Deployment { .as_deref() .unwrap_or(self.checksum.as_str()) } + + /// Find a given metadata key in the base commit, which must hold a non-empty string. + pub fn find_base_commitmeta_string<'a>(&'a self, k: &str) -> Result<&'a str> { + let v = self.base_commit_meta.get(k); + if let Some(v) = v { + match v { + serde_json::Value::String(v) => { + if v.is_empty() { + Err(format!("Invalid empty {} metadata key", k).into()) + } else { + Ok(v) + } + } + _ => Err(format!("Invalid non-string {} metadata key", k).into()), + } + } else { + Err(format!("No {} metadata key", k).into()) + } + } } fn cli_cmd(c: &CliClient) -> Command { diff --git a/rust/rpmostree-client/tests/parse.rs b/rust/rpmostree-client/tests/parse.rs index a4eb8283..1cb8f95a 100644 --- a/rust/rpmostree-client/tests/parse.rs +++ b/rust/rpmostree-client/tests/parse.rs @@ -13,5 +13,12 @@ fn parse_workstation() -> Result<()> { booted.get_base_commit(), "229387d3c0bb8ad698228ca5702eca72aed8b298a7c800be1dc72bab160a9f7f" ); + assert!(booted.find_base_commitmeta_string("foo").is_err()); + assert_eq!( + booted + .find_base_commitmeta_string("coreos-assembler.config-gitrev") + .unwrap(), + "80966f951c766846da070b4c168b9170c61513e2" + ); Ok(()) }