Merge pull request #2730 from cgwalters/client-methods

rust/client: Make status be a method
This commit is contained in:
Luca Bruno 2021-04-08 13:31:30 +00:00 committed by GitHub
commit 5725eb023a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 35 deletions

View File

@ -98,14 +98,16 @@ impl Deployment {
} }
} }
fn cli_cmd(c: &CliClient) -> Command { impl CliClient {
/// Create an invocation of the client binary
fn cli_cmd(&self) -> Command {
let mut cmd = Command::new("rpm-ostree"); let mut cmd = Command::new("rpm-ostree");
cmd.env("RPMOSTREE_CLIENT_ID", c.agent_id.as_str()); cmd.env("RPMOSTREE_CLIENT_ID", self.agent_id.as_str());
cmd cmd
} }
/// Gather a snapshot of the system status. /// Gather a snapshot of the system status.
pub fn query_status(c: &CliClient) -> Result<Status> { pub fn query_status(&self) -> Result<Status> {
// Retry on temporary activation failures, see // Retry on temporary activation failures, see
// https://github.com/coreos/rpm-ostree/issues/2531 // https://github.com/coreos/rpm-ostree/issues/2531
let pause = std::time::Duration::from_secs(1); let pause = std::time::Duration::from_secs(1);
@ -113,7 +115,8 @@ pub fn query_status(c: &CliClient) -> Result<Status> {
let mut retries = 0; let mut retries = 0;
let cmd_res = loop { let cmd_res = loop {
retries += 1; retries += 1;
let res = cli_cmd(c) let res = self
.cli_cmd()
.args(&["status", "--json"]) .args(&["status", "--json"])
.output() .output()
.context("failed to spawn 'rpm-ostree status'")?; .context("failed to spawn 'rpm-ostree status'")?;
@ -135,3 +138,4 @@ pub fn query_status(c: &CliClient) -> Result<Status> {
Ok(serde_json::from_slice(&cmd_res.stdout) Ok(serde_json::from_slice(&cmd_res.stdout)
.context("failed to parse 'rpm-ostree status' output")?) .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(())
} }