rust/client: Add methods to find/require booted deployment

This is a common need.
This commit is contained in:
Colin Walters 2021-03-05 18:11:03 +00:00 committed by OpenShift Merge Robot
parent 1662f246fd
commit 14c17cfe52
2 changed files with 14 additions and 1 deletions

View File

@ -56,6 +56,19 @@ pub struct Deployment {
pub version: Option<String>,
}
impl Status {
/// Find the booted deployment, if any.
pub fn find_booted(&self) -> Option<&Deployment> {
self.deployments.iter().find(|d| d.booted)
}
/// Find the booted deployment.
pub fn require_booted(&self) -> Result<&Deployment> {
self.find_booted()
.ok_or_else(|| format!("No booted deployment").into())
}
}
fn cli_cmd(c: &CliClient) -> Command {
let mut cmd = Command::new("rpm-ostree");
cmd.env("RPMOSTREE_CLIENT_ID", c.agent_id.as_str());

View File

@ -7,7 +7,7 @@ fn parse_workstation() -> Result<()> {
let data = include_str!("fixtures/workstation-status.json");
let state: &rpmostree_client::Status = &serde_json::from_str(data)?;
assert_eq!(state.deployments.len(), 2);
let booted = &state.deployments[0];
let booted = state.require_booted().unwrap();
assert_eq!(booted.version.as_ref().unwrap().as_str(), "33.21");
Ok(())
}