|
1 | | -use anyhow::Result; |
| 1 | +use anyhow::{Context, Result}; |
2 | 2 | use console::{style, Term}; |
3 | 3 | use std::process::Command; |
4 | 4 | use std::time::Instant; |
@@ -66,36 +66,32 @@ impl TaskRunner { |
66 | 66 | } |
67 | 67 | } |
68 | 68 |
|
69 | | -pub fn run(mut command: Command, verbose: bool) -> Result<()> { |
70 | | - fn print_error(command: &Command, status: Option<i32>) { |
71 | | - let program = command.get_program().to_str().unwrap(); |
72 | | - let args = command |
73 | | - .get_args() |
74 | | - .map(|arg| arg.to_str().unwrap()) |
75 | | - .collect::<Vec<_>>() |
76 | | - .join(" "); |
| 69 | +pub fn run(command: &mut Command, verbose: bool) -> Result<()> { |
| 70 | + fn format_error(command: &Command, status: Option<i32>) -> String { |
77 | 71 | let status = if let Some(code) = status { |
78 | 72 | format!(" exited with {code}") |
79 | 73 | } else { |
80 | 74 | Default::default() |
81 | 75 | }; |
82 | | - println!("{} {} {} {}", style("[ERROR]").red(), program, args, status); |
| 76 | + format!("{} `{:?}`{}", style("[ERROR]").red(), command, status) |
83 | 77 | } |
84 | 78 | if !verbose { |
85 | | - let output = command.output()?; |
| 79 | + let output = command |
| 80 | + .output() |
| 81 | + .with_context(|| format_error(command, None))?; |
86 | 82 | if !output.status.success() { |
87 | | - print_error(&command, output.status.code()); |
88 | 83 | let stdout = std::str::from_utf8(&output.stdout)?; |
89 | 84 | print!("{stdout}"); |
90 | 85 | let stderr = std::str::from_utf8(&output.stderr)?; |
91 | 86 | print!("{stderr}"); |
92 | | - std::process::exit(1); |
| 87 | + anyhow::bail!("{}", format_error(command, output.status.code())); |
93 | 88 | } |
94 | 89 | } else { |
95 | | - let status = command.status()?; |
| 90 | + let status = command |
| 91 | + .status() |
| 92 | + .with_context(|| format_error(command, None))?; |
96 | 93 | if !status.success() { |
97 | | - print_error(&command, status.code()); |
98 | | - std::process::exit(1); |
| 94 | + anyhow::bail!("{}", format_error(command, status.code())); |
99 | 95 | } |
100 | 96 | } |
101 | 97 | Ok(()) |
|
0 commit comments