Skip to content

Commit

Permalink
fixup! 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 271c4d6 commit 806a3c2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ 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};
use std::thread;
use std::process::Command;
use std::process::Output;
use std::io;

use self::ansi_term::Colour::{Red, Green, Yellow, Black};
use self::ansi_term::ANSIString;
Expand Down Expand Up @@ -39,13 +36,11 @@ fn task_failure(task: Task, output: Output, json: bool) {
}
}

fn buildTaskOutput(output: io::Result<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,
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());
}
}

Expand All @@ -66,8 +61,8 @@ pub fn run(tasks: Vec<Task>, cwd_path: String, json_output: bool) -> bool {
.current_dir(path)
.output()
.expect("command failed");
// let cloned_output = command_output.clone();
list.push(buildTaskOutput(command_output, task_data));
let cloned_output = command_output.clone();
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 @@ -77,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 806a3c2

Please sign in to comment.