rust/client: Make status be a method

In trying to use the API in ostree's tests, this feels like an obvious
change.
This commit is contained in:
Colin Walters 2021-04-07 18:55:47 +00:00
parent 45a59fc7f8
commit 182cae05d6
2 changed files with 39 additions and 35 deletions

View File

@ -98,40 +98,44 @@ impl Deployment {
} }
} }
fn cli_cmd(c: &CliClient) -> Command { impl CliClient {
let mut cmd = Command::new("rpm-ostree"); /// Create an invocation of the client binary
cmd.env("RPMOSTREE_CLIENT_ID", c.agent_id.as_str()); fn cli_cmd(&self) -> Command {
cmd let mut cmd = Command::new("rpm-ostree");
} cmd.env("RPMOSTREE_CLIENT_ID", self.agent_id.as_str());
cmd
/// Gather a snapshot of the system status.
pub fn query_status(c: &CliClient) -> Result<Status> {
// Retry on temporary activation failures, see
// https://github.com/coreos/rpm-ostree/issues/2531
let pause = std::time::Duration::from_secs(1);
let max_retries = 10;
let mut retries = 0;
let cmd_res = loop {
retries += 1;
let res = cli_cmd(c)
.args(&["status", "--json"])
.output()
.context("failed to spawn 'rpm-ostree status'")?;
if res.status.success() || retries >= max_retries {
break res;
}
std::thread::sleep(pause);
};
if !cmd_res.status.success() {
return Err(format!(
"running 'rpm-ostree status' failed: {}",
String::from_utf8_lossy(&cmd_res.stderr)
)
.into());
} }
Ok(serde_json::from_slice(&cmd_res.stdout) /// Gather a snapshot of the system status.
.context("failed to parse 'rpm-ostree status' output")?) pub fn query_status(&self) -> Result<Status> {
// Retry on temporary activation failures, see
// https://github.com/coreos/rpm-ostree/issues/2531
let pause = std::time::Duration::from_secs(1);
let max_retries = 10;
let mut retries = 0;
let cmd_res = loop {
retries += 1;
let res = self
.cli_cmd()
.args(&["status", "--json"])
.output()
.context("failed to spawn 'rpm-ostree status'")?;
if res.status.success() || retries >= max_retries {
break res;
}
std::thread::sleep(pause);
};
if !cmd_res.status.success() {
return Err(format!(
"running 'rpm-ostree status' failed: {}",
String::from_utf8_lossy(&cmd_res.stderr)
)
.into());
}
Ok(serde_json::from_slice(&cmd_res.stdout)
.context("failed to parse 'rpm-ostree status' output")?)
}
} }

View File

@ -227,7 +227,7 @@ fn update_os_tree(opts: &SyntheticUpgradeOpts) -> Result<()> {
// to update the client bindings when adding new fields. // to update the client bindings when adding new fields.
fn validate_parse_status() -> Result<()> { fn validate_parse_status() -> Result<()> {
let c = rpmostree_client::CliClient::new("tests"); let c = rpmostree_client::CliClient::new("tests");
let s = rpmostree_client::query_status(&c).map_err(anyhow::Error::msg)?; let s = c.query_status().map_err(anyhow::Error::msg)?;
assert_ne!(s.deployments.len(), 0); assert_ne!(s.deployments.len(), 0);
Ok(()) Ok(())
} }