Skip to content

Commit

Permalink
do string splitting nonsense to get it working
Browse files Browse the repository at this point in the history
  • Loading branch information
tomeichlersmith committed Sep 2, 2024
1 parent 187c427 commit 4e93608
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/cmdrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,34 @@ impl CmdRun {

// This method is public for unit tests
pub fn run_cmdrun(command: String, working_dir: &str, inline: bool) -> Result<String> {
let (command, correct_exit_code): (String, Option<i32>) = if let Some(first_word) = command.split_whitespace().next() {
if first_word.starts_with('-') {
let (_, exit_code) = first_word.rsplit_once('-').unwrap_or(("","0"));
(command.split_whitespace().skip(1).collect::<String>(), Some(exit_code.parse()?))
} else {
(command, None)
}
} else {
(command, None)
};

let output = Command::new(LAUNCH_SHELL_COMMAND)
.args([LAUNCH_SHELL_FLAG, &command])
.current_dir(working_dir)
.output()
.with_context(|| "Fail to run shell")?;

match (output.status.code(), correct_exit_code) {
(None, _) => return Err(anyhow::Error::msg(format!("'{command}' was ended before completing."))),
(Some(code), Some(correct_code)) => if code != correct_code {
return Err(
anyhow::Error::msg(format!("'{command}' returned exit code {code} instead of {correct_code}."))
.context(String::from_utf8_lossy(&output.stderr).to_string())
)
},
(Some(code), None) => ()
}

let stdout = Self::format_whitespace(String::from_utf8_lossy(&output.stdout), inline);

// let stderr = String::from_utf8_lossy(&output.stderr).to_string();
Expand Down

0 comments on commit 4e93608

Please sign in to comment.