client: Add API to fetch base commit metadata

Also desired by Zincati.
This commit is contained in:
Colin Walters 2021-03-05 18:22:48 +00:00 committed by OpenShift Merge Robot
parent 5b647af14a
commit 8f77970683
2 changed files with 26 additions and 0 deletions

View File

@ -76,6 +76,25 @@ impl Deployment {
.as_deref() .as_deref()
.unwrap_or(self.checksum.as_str()) .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 { fn cli_cmd(c: &CliClient) -> Command {

View File

@ -13,5 +13,12 @@ fn parse_workstation() -> Result<()> {
booted.get_base_commit(), booted.get_base_commit(),
"229387d3c0bb8ad698228ca5702eca72aed8b298a7c800be1dc72bab160a9f7f" "229387d3c0bb8ad698228ca5702eca72aed8b298a7c800be1dc72bab160a9f7f"
); );
assert!(booted.find_base_commitmeta_string("foo").is_err());
assert_eq!(
booted
.find_base_commitmeta_string("coreos-assembler.config-gitrev")
.unwrap(),
"80966f951c766846da070b4c168b9170c61513e2"
);
Ok(()) Ok(())
} }