Skip to content

Commit 28e6075

Browse files
committed
Show error of shell command
1 parent a901678 commit 28e6075

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/action_menu.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Result;
1+
use anyhow::{anyhow, Context, Result};
22
use std::process::{Command, Stdio};
33

44
use crate::logs::log;
@@ -74,13 +74,29 @@ pub fn generate_known_actions() -> Vec<MenuAction> {
7474
pub fn execute_shell_operation(path: &String, command_template: &str) -> Result<()> {
7575
let cmd = String::from(command_template).replace("{}", path);
7676
log(format!("Executing command: {:?}", cmd).as_str());
77-
let mut c = Command::new("sh")
77+
let c = Command::new("sh")
7878
.arg("-c")
7979
.arg(cmd.clone())
8080
.stdin(Stdio::inherit())
8181
.stderr(Stdio::piped())
8282
.stdout(Stdio::piped())
83-
.spawn()?;
84-
c.wait()?;
83+
.spawn()
84+
.context("failed to start a command")?;
85+
let output = c
86+
.wait_with_output()
87+
.context("failed to read command output")?;
88+
89+
if !output.status.success() {
90+
let error = format!(
91+
"Failed to execute command: {:?}, {}\n{}\n{}",
92+
cmd,
93+
output.status,
94+
String::from_utf8_lossy(&output.stderr),
95+
String::from_utf8_lossy(&output.stdout),
96+
);
97+
log(error.as_str());
98+
return Err(anyhow!(error));
99+
}
100+
85101
Ok(())
86102
}

src/update.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,12 @@ pub fn on_key_tree(app: &mut App, key_event: KeyEvent) {
3434
KeyCode::Char('u') if key_event.modifiers == KeyModifiers::CONTROL => {
3535
app.clear_search_text();
3636
}
37-
KeyCode::Backspace => {
38-
app.backspace_search_text();
39-
}
37+
KeyCode::Backspace => app.backspace_search_text(),
4038
KeyCode::Char('w') if key_event.modifiers == KeyModifiers::CONTROL => {
4139
app.backspace_search_text();
4240
}
43-
KeyCode::Char(c) => {
44-
app.type_search_text(c);
45-
}
46-
_ => {
47-
log(format!("Key event: {:?}", key_event).as_str());
48-
}
41+
KeyCode::Char(c) => app.type_search_text(c),
42+
_ => log(format!("Key event: {:?}", key_event).as_str()),
4943
};
5044
}
5145

0 commit comments

Comments
 (0)