Skip to content

Commit 93872cb

Browse files
feat(logs): Pipe stdout/stderr to the log file (#16)
1 parent 1eece9d commit 93872cb

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/main.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use chrono::Utc;
44
use serde::{Deserialize, Serialize};
55
use std::fs;
66
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
7-
use std::process::{Command, Output, Stdio};
7+
use std::process::{Command, Output};
88
use std::str;
99
use std::thread;
1010
#[macro_use]
@@ -64,11 +64,15 @@ impl Project {
6464
}
6565

6666
// TODO: handle failure by returning Result
67-
fn run_command(path: &str, command: &str) -> Output {
67+
fn run_command(path: &str, command: &str, log: &std::fs::File) -> Output {
68+
let stdout = log.try_clone().expect("Failed to clone log file (stdout)");
69+
let stderr = log.try_clone().expect("Failed to clone log file (stderr)");
70+
6871
Command::new("sh")
6972
.arg("-c")
7073
.arg(command)
71-
.stdout(Stdio::piped())
74+
.stdout(stdout)
75+
.stderr(stderr)
7276
.current_dir(path)
7377
.spawn()
7478
.expect("failed to execute child")
@@ -96,16 +100,7 @@ fn run_project(project: Project, mut log: std::fs::File) {
96100
.unwrap();
97101

98102
let path = shellexpand::tilde(&project.path).into_owned();
99-
let output = run_command(&path, &command);
100-
101-
// TODO: Stream command stdout to log file instead of parse and log the
102-
// whole response
103-
match str::from_utf8(&output.stdout) {
104-
Ok(stdout) => log.write_all(stdout.as_bytes()).unwrap(),
105-
Err(error) => log
106-
.write_all(format!("Failed to parse stdout: {:?}\n", error).as_bytes())
107-
.unwrap(),
108-
}
103+
let output = run_command(&path, &command, &log);
109104

110105
match (output.status.success(), output.status.code()) {
111106
(true, _) => (),

0 commit comments

Comments
 (0)