wasefire_cli_tools/
cmd.rs1use std::os::unix::process::CommandExt;
18use std::process::Output;
19
20use anyhow::{Context, Result, ensure};
21use tokio::process::{Child, Command};
22
23pub fn spawn(command: &mut Command) -> Result<Child> {
25 debug!("{:?}", command.as_std());
26 command.spawn().with_context(|| context(command))
27}
28
29pub async fn status(command: &mut Command) -> Result<i32> {
31 let status = spawn(command)?.wait().await.with_context(|| context(command))?;
32 status.code().context("no error code")
33}
34
35pub async fn exit_status(command: &mut Command) -> Result<()> {
37 let code = status(command).await?;
38 if code != 0 {
39 std::process::exit(code);
40 }
41 Ok(())
42}
43
44pub async fn execute(command: &mut Command) -> Result<()> {
46 let code = status(command).await?;
47 ensure!(code == 0, "failed with code {code}");
48 Ok(())
49}
50
51pub fn replace(mut command: Command) -> ! {
53 debug!("{:?}", command.as_std());
54 panic!("{}", command.as_std_mut().exec());
55}
56
57pub async fn output(command: &mut Command) -> Result<Output> {
59 debug!("{:?}", command.as_std());
60 let output = command.output().await.with_context(|| context(command))?;
61 ensure!(output.status.success(), "failed with status {}", output.status);
62 Ok(output)
63}
64
65pub async fn output_line(command: &mut Command) -> Result<String> {
67 let mut output = output(command).await?;
68 assert!(output.stderr.is_empty());
69 assert_eq!(output.stdout.pop(), Some(b'\n'));
70 Ok(String::from_utf8(output.stdout)?)
71}
72
73fn context(command: &Command) -> String {
74 format!("executing {:?}", command.as_std())
75}