Skip to content

Commit 1ff907b

Browse files
committed
make command debug prettier
1 parent bbb643f commit 1ff907b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub fn cargo_metadata_with_args(
111111
.ok()
112112
.unwrap_or_else(|| "cargo".to_string()),
113113
);
114-
command.arg("metadata").arg("--format-version=1");
114+
command.arg("metadata").args(&["--format-version", "1"]);
115115
if let Some(cd) = cd {
116116
command.current_dir(cd);
117117
}

src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn install_panic_hook() -> Result<()> {
1010

1111
#[derive(Debug, thiserror::Error)]
1212
pub enum CommandError {
13-
#[error("`{command}` failed with exit code: {status}")]
13+
#[error("`{command}` failed with {status}")]
1414
NonZeroExitCode {
1515
status: std::process::ExitStatus,
1616
command: String,

src/extensions.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,38 @@ pub trait CommandExt {
1919
) -> Result<ExitStatus, CommandError>;
2020
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String>;
2121
fn run_and_get_output(&mut self, verbose: bool) -> Result<std::process::Output>;
22+
fn command_pretty(&self) -> String;
2223
}
2324

2425
impl CommandExt for Command {
26+
fn command_pretty(&self) -> String {
27+
// a dummy implementor of display to avoid using unwraps
28+
struct C<'c>(&'c Command);
29+
impl std::fmt::Display for C<'_> {
30+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31+
let cmd = self.0;
32+
write!(f, "{}", cmd.get_program().to_string_lossy())?;
33+
let args = cmd.get_args();
34+
if args.len() > 1 {
35+
write!(f, " ")?;
36+
write!(
37+
f,
38+
"{}",
39+
shell_words::join(args.map(|o| o.to_string_lossy()))
40+
)?;
41+
}
42+
Ok(())
43+
}
44+
}
45+
format!("{}", C(self))
46+
}
47+
2548
fn print_verbose(&self, verbose: bool) {
2649
if verbose {
2750
if let Some(cwd) = self.get_current_dir() {
28-
println!("+ {:?} {:?}", cwd, self);
51+
println!("+ {:?} {}", cwd, self.command_pretty());
2952
} else {
30-
println!("+ {:?}", self);
53+
println!("+ {}", self.command_pretty());
3154
}
3255
}
3356
}
@@ -42,7 +65,7 @@ impl CommandExt for Command {
4265
} else {
4366
Err(CommandError::NonZeroExitCode {
4467
status,
45-
command: format!("{self:?}"),
68+
command: self.command_pretty(),
4669
stderr: output.map(|out| out.stderr.clone()).unwrap_or_default(),
4770
stdout: output.map(|out| out.stdout.clone()).unwrap_or_default(),
4871
})
@@ -68,7 +91,7 @@ impl CommandExt for Command {
6891
self.status()
6992
.map_err(|e| CommandError::CouldNotExecute {
7093
source: Box::new(e),
71-
command: format!("{self:?}"),
94+
command: self.command_pretty(),
7295
})
7396
.map_err(Into::into)
7497
}
@@ -91,7 +114,7 @@ impl CommandExt for Command {
91114
self.output().map_err(|e| {
92115
CommandError::CouldNotExecute {
93116
source: Box::new(e),
94-
command: format!("{self:?}"),
117+
command: self.command_pretty(),
95118
}
96119
.to_section_report()
97120
})

0 commit comments

Comments
 (0)