Skip to content

Commit 8d87d2a

Browse files
authored
Merge pull request #733 from cgwalters/command-ext
utils: Add a little CommandRunExt helper trait
2 parents 741e904 + 858d886 commit 8d87d2a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/src/install.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::mount::Filesystem;
4747
use crate::spec::ImageReference;
4848
use crate::store::Storage;
4949
use crate::task::Task;
50-
use crate::utils::sigpolicy_from_opts;
50+
use crate::utils::{sigpolicy_from_opts, CommandRunExt};
5151

5252
/// The default "stateroot" or "osname"; see https://github.com/ostreedev/ostree/issues/2794
5353
const STATEROOT_DEFAULT: &str = "default";
@@ -587,10 +587,9 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result
587587
("sysroot.bootprefix", "true"),
588588
("sysroot.readonly", "true"),
589589
] {
590-
Task::new("Configuring ostree repo", "ostree")
590+
Command::new("ostree")
591591
.args(["config", "--repo", "ostree/repo", "set", k, v])
592-
.cwd(rootfs_dir)?
593-
.quiet()
592+
.cwd_dir(rootfs_dir.try_clone()?)
594593
.run()?;
595594
}
596595
Task::new("Initializing sysroot", "ostree")

lib/src/utils.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ use ostree::glib;
1010
use ostree_ext::container::SignatureSource;
1111
use ostree_ext::ostree;
1212

13+
/// Helpers intended for [`std::process::Command`].
14+
pub(crate) trait CommandRunExt {
15+
fn run(&mut self) -> Result<()>;
16+
}
17+
18+
impl CommandRunExt for Command {
19+
/// Synchronously execute the child, and return an error if the child exited unsuccessfully.
20+
fn run(&mut self) -> Result<()> {
21+
let st = self.status()?;
22+
if !st.success() {
23+
// Note that we intentionally *don't* include the command string
24+
// in the output; we leave it to the caller to add that if they want,
25+
// as it may be verbose.
26+
anyhow::bail!(format!("Subprocess failed: {st:?}"))
27+
}
28+
Ok(())
29+
}
30+
}
31+
1332
/// Try to look for keys injected by e.g. rpm-ostree requesting machine-local
1433
/// changes; if any are present, return `true`.
1534
pub(crate) fn origin_has_rpmostree_stuff(kf: &glib::KeyFile) -> bool {
@@ -190,3 +209,9 @@ fn test_sigpolicy_from_opts() {
190209
SignatureSource::ContainerPolicyAllowInsecure
191210
);
192211
}
212+
213+
#[test]
214+
fn command_run_ext() {
215+
Command::new("true").run().unwrap();
216+
assert!(Command::new("false").run().is_err());
217+
}

0 commit comments

Comments
 (0)