From 806a3c21125f4e3204486adbbbe8129faa22b62d Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Wed, 12 Dec 2018 10:52:29 +0100 Subject: [PATCH] fixup! Refactor task executor implementation --- src/execute.rs | 25 ++++++++----------------- src/task_output.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/execute.rs b/src/execute.rs index 2892658..436a61c 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -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; @@ -39,13 +36,11 @@ fn task_failure(task: Task, output: Output, json: bool) { } } -fn buildTaskOutput(output: io::Result, 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>>, 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()); } } @@ -66,8 +61,8 @@ pub fn run(tasks: Vec, 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), @@ -77,10 +72,6 @@ pub fn run(tasks: Vec, 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 } \ No newline at end of file diff --git a/src/task_output.rs b/src/task_output.rs index 42c371e..ee17976 100644 --- a/src/task_output.rs +++ b/src/task_output.rs @@ -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, @@ -16,3 +19,13 @@ pub type Tasks = Vec; pub struct SerializableOutput { pub tasks: Vec, } + +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, + } +}