Skip to content

Commit

Permalink
refact: rename commands args
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoliveira committed Jun 9, 2023
1 parent 3479296 commit 5b20da4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
25 changes: 8 additions & 17 deletions src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
use std::process::{Child, Command};

pub fn execute(command_line: &String) -> Result<(), String> {
pub fn execute(command: &String) -> Result<(), String> {
let shell = std::env::var("SHELL").unwrap_or(String::from("/bin/sh"));
let mut cmd = Command::new(shell);
match cmd.arg("-c").arg(command_line).status() {
Err(error) => Err(format!(
"Command {} has errored with {}",
command_line, error
)),
match cmd.arg("-c").arg(command).status() {
Err(error) => Err(format!("Command {} has errored with {}", command, error)),
Ok(status) => {
if status.success() {
Ok(())
} else {
Err(format!(
"Command {} has failed with {}",
command_line, status
))
Err(format!("Command {} has failed with {}", command, status))
}
}
}
}

pub fn spawn_command(command_line: String) -> Result<Child, String> {
pub fn spawn(command: String) -> Result<Child, String> {
let shell = std::env::var("SHELL").unwrap_or(String::from("/bin/sh"));
let mut cmd = Command::new(shell);

match cmd.arg("-c").arg(&command_line).spawn() {
match cmd.arg("-c").arg(&command).spawn() {
Ok(child) => Ok(child),

Err(error) => Err(format!(
"Command {} has errored with {}",
command_line, error
)),
Err(error) => Err(format!("Command {} has errored with {}", command, error)),
}
}

#[test]
fn it_spawn_a_command_returning_a_child_ref() {
let result = match spawn_command(String::from("echo 'foo'")) {
let result = match spawn(String::from("echo 'foo'")) {
Ok(mut child) => child.wait().expect("fail to wait"),
Err(err) => panic!("{:?}", err),
};
Expand Down
4 changes: 2 additions & 2 deletions src/workers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cmd::spawn_command;
use cmd::spawn;
use rules::{self, Rules};
use std::sync::mpsc::channel;
use std::sync::mpsc::{Sender, TryRecvError};
Expand Down Expand Up @@ -32,7 +32,7 @@ impl Worker {
break;
}
stdout::info(&format!("---- running: {:?} ----", task));
let mut child = match spawn_command(task.clone()) {
let mut child = match spawn(task.clone()) {
Ok(child) => child,
Err(err) => {
stdout::error(&format!("failed to create command: {:?}", err));
Expand Down

0 comments on commit 5b20da4

Please sign in to comment.