Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the case when a command does not exist #27

Merged
merged 1 commit into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ async fn wait_for(addresses: Vec<String>, quiet: bool) {
}
}

pub async fn run() -> Result<(), &'static str> {
fn exec_command(command: &str, args: &[String]) -> Result<(), String> {
if Command::new(command).args(args).spawn().is_err() {
let err = format!("Command not found: {}", command);
return Err(err);
}

Ok(())
}

pub async fn run() -> Result<(), String> {
let args = Args::parse();

let thread = thread::spawn(move || async move {
Expand All @@ -64,13 +73,32 @@ pub async fn run() -> Result<(), &'static str> {
});

if thread.join().unwrap().await.is_err() {
return Err("Connection timeout, could not connect to the addresses.");
return Err(String::from(
"Connection timeout, could not connect to the addresses.",
));
}

Command::new(&args.cmd[0])
.args(&args.cmd[1..])
.spawn()
.expect("Failed to run the command.");
exec_command(&args.cmd[0], &args.cmd[1..])?;

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn command_not_found() {
let args = Args {
timeout: 30,
addresses: vec![String::from("google.com:80")],
cmd: vec![String::from("not_a_command")],
quiet: false,
};

assert_eq!(
exec_command(&args.cmd[0], &args.cmd[1..]),
Err(String::from("Command not found: not_a_command"))
);
}
}
Loading