Skip to content

Commit

Permalink
Refactor task executor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dottorblaster committed Dec 12, 2018
1 parent 0c08dac commit 73671a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ extern crate ansi_term;
extern crate serde_json;

use parse_config::Task;
use task_output::TaskOutput;
use task_output::Tasks;
use task_output::SerializableOutput;
use task_output;
use std::sync::{Mutex, Arc};
Expand Down Expand Up @@ -38,6 +36,14 @@ fn task_failure(task: Task, output: Output, json: bool) {
}
}

fn print_json(outputs: Arc<Mutex<Vec<task_output::TaskOutput>>>, json: bool) {
if json == true {
let slice = &*outputs.lock().unwrap();
let serializable_output = SerializableOutput { tasks: slice.to_vec() };
println!("{}", serde_json::to_string(&serializable_output).unwrap());
}
}

pub fn run(tasks: Vec<Task>, cwd_path: String, json_output: bool) -> bool {
let outputs = Arc::new(Mutex::new(task_output::Tasks::with_capacity(tasks.len())));
let mut handles = Vec::with_capacity(tasks.len());
Expand All @@ -56,13 +62,7 @@ pub fn run(tasks: Vec<Task>, cwd_path: String, json_output: bool) -> bool {
.output()
.expect("command failed");
let cloned_output = command_output.clone();
list.push(TaskOutput {
outcome: String::from_utf8(cloned_output.stdout).unwrap(),
code: cloned_output.status.code().unwrap().to_string(),
name: task_data.name,
description: task_data.description,
command: task_data.command,
});
list.push(task_output::build_task_output(cloned_output, task_data));
match command_output.status.code() {
Some(0) => task_success(data, command_output, json_output),
Some(_) => task_failure(data, command_output, json_output),
Expand All @@ -72,10 +72,6 @@ pub fn run(tasks: Vec<Task>, cwd_path: String, json_output: bool) -> bool {
handles.push(child);
}
for handle in handles { handle.join().unwrap(); }
if json_output == true {
let slice = &*outputs.lock().unwrap();
let serializable_output = SerializableOutput { tasks: slice.to_vec() };
println!("{}", serde_json::to_string(&serializable_output).unwrap());
}
print_json(outputs, json_output);
true
}
13 changes: 13 additions & 0 deletions src/task_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
extern crate serde;
extern crate serde_json;

use parse_config::Task;
use std::process::Output;

#[derive(Serialize, Clone)]
pub struct TaskOutput {
pub outcome: String,
Expand All @@ -16,3 +19,13 @@ pub type Tasks = Vec<TaskOutput>;
pub struct SerializableOutput {
pub tasks: Vec<TaskOutput>,
}

pub fn build_task_output(output: Output, task: Task) -> TaskOutput {
TaskOutput {
outcome: String::from_utf8(output.stdout).unwrap(),
code: output.status.code().unwrap().to_string(),
name: task.name,
description: task.description,
command: task.command,
}
}

0 comments on commit 73671a5

Please sign in to comment.