Skip to content

Commit 559c68e

Browse files
Abstract log writing a tiny bit.
1 parent ade35a8 commit 559c68e

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

src/deploy_task.rs

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,29 @@ use repo_config::{RepoConfig, DeployMethod};
66
use server_config::Environment;
77
use std::env;
88
use std::error::Error;
9+
use std::fmt::Display;
910
use std::fs::File;
10-
use std::io::Write;
11+
use std::io::{Write, Result};
1112
use std::path::Path;
1213
use task_manager::Runnable;
1314
use users;
1415
use uuid::Uuid;
1516

17+
struct LogWriter {
18+
file: File
19+
}
20+
21+
impl LogWriter {
22+
fn new(path: &Path) -> Result<LogWriter> {
23+
Ok(LogWriter { file: try!(File::create(path)) })
24+
}
25+
26+
#[allow(unused_must_use)]
27+
fn write<T: AsRef<str> + Display>(&mut self, msg: T) {
28+
self.file.write_all(format!("{}\n", msg).as_bytes());
29+
}
30+
}
31+
1632
pub struct DeployTask {
1733
pub repo: GitRepo,
1834
pub id: Uuid,
@@ -25,7 +41,6 @@ impl Runnable for DeployTask {
2541

2642
// TODO: this is a god damn mess and seriously needs to be refactored,
2743
// especially all of the logging.
28-
#[allow(unused_must_use)]
2944
fn run(&mut self) {
3045
let task_id = self.id.to_string();
3146

@@ -43,29 +58,28 @@ impl Runnable for DeployTask {
4358

4459
// Truncate the logfile and write "task running..."
4560
let logfile_path = Path::new(&self.logdir).join(format!("{}.log", task_id));
46-
let mut logfile = match File::create(&logfile_path) {
61+
let mut logger = match LogWriter::new(&logfile_path) {
4762
Ok(logfile) => logfile,
4863
Err(_) => return println!("[{}]: could not open logfile for writing", &task_id),
4964
};
50-
logfile.write_all(b"\ntask running...\n");
51-
5265
// Log the current user
53-
logfile.write_all(format!("system user: {}\n\n", users::get_current_username().unwrap_or("<none>".to_owned())).as_bytes());
66+
logger.write(format!("system user: {}\n", users::get_current_username().unwrap_or("<none>".to_owned())));
5467

5568
// Log the hookshot environment variables
56-
logfile.write_all(format!("hookshot environment:\n---------------------\n{}\n", format_environment(&self.env)).as_bytes());
69+
logger.write(format!("hookshot environment:\n---------------------\n{}", format_environment(&self.env)));
5770

5871
// Log the system environment variables
59-
logfile.write_all(format!("system environment:\n-------------------\n{}\n", format_os_environment()).as_bytes());
72+
logger.write(format!("system environment:\n-------------------\n{}", format_os_environment()));
6073

6174
// Log what time the task started.
6275
let time_task_started = UTC::now();
63-
logfile.write_all(format!("started: {}\n", time_task_started).as_bytes());
76+
logger.write(format!("started: {}", time_task_started));
6477

6578
if let Err(git_error) = self.repo.get_latest() {
6679
let stderr = String::from_utf8(git_error.output.unwrap().stderr).unwrap();
6780
let err = format!("{}: {}", git_error.desc, stderr);
68-
logfile.write_all(format!("{}", err).as_bytes());
81+
82+
logger.write(format!("{}", err));
6983
return println!("[{}]: {}", task_id, err);
7084
}
7185

@@ -76,8 +90,9 @@ impl Runnable for DeployTask {
7690
self.repo.remote_path,
7791
e.description(),
7892
e.related_branch().unwrap_or("None"));
79-
logfile.write_all(format!("{}", err).as_bytes());
80-
return println!("[{}]: {}", &task_id, err);
93+
94+
logger.write(format!("{}", err));
95+
return println!("[{}]: {}", task_id, err);
8196
}
8297
Ok(config) => config,
8398
};
@@ -87,8 +102,9 @@ impl Runnable for DeployTask {
87102
let ref_config = match config.lookup(self.repo.reftype, &self.repo.refstring) {
88103
None => {
89104
let err = format!("No config for ref '{}'", &self.repo.refstring);
90-
logfile.write_all(format!("{}", err).as_bytes());
91-
return println!("[{}]: {}", &task_id, err);
105+
106+
logger.write(format!("{}", err));
107+
return println!("[{}]: {}", task_id, err);
92108
}
93109
Some(config) => config,
94110
};
@@ -99,7 +115,8 @@ impl Runnable for DeployTask {
99115
DeployMethod::Ansible => match ref_config.ansible_task() {
100116
None => {
101117
let err = format!("No task for ref '{}'", &self.repo.refstring);
102-
logfile.write_all(format!("{}", err).as_bytes());
118+
119+
logger.write(format!("{}", err));
103120
return println!("[{}]: {}", &task_id, err);
104121
}
105122
Some(task) => {
@@ -111,7 +128,8 @@ impl Runnable for DeployTask {
111128
DeployMethod::Makefile => match ref_config.make_task() {
112129
None => {
113130
let err = format!("No task for ref '{}'", &self.repo.refstring);
114-
logfile.write_all(format!("{}", err).as_bytes());
131+
132+
logger.write(format!("{}", err));
115133
return println!("[{}]: {}", &task_id, err);
116134
}
117135
Some(task) => {
@@ -129,7 +147,7 @@ impl Runnable for DeployTask {
129147
let err = format!("task failed: {} ({})",
130148
e.desc,
131149
e.detail.unwrap_or(String::from("")));
132-
logfile.write_all(format!("{}", err).as_bytes());
150+
logger.write(format!("{}", err));
133151
return println!("[{}]: {}", &task_id, err);
134152
}
135153
};
@@ -154,15 +172,16 @@ impl Runnable for DeployTask {
154172
// Log what time the task ended and how long it took
155173
let time_task_ended = UTC::now();
156174
let duration = time_task_ended - time_task_started;
157-
logfile.write_all(format!("task finished: {}\n", time_task_ended).as_bytes());
158-
logfile.write_all(format!("duration: {}...\n\n", format_duration(duration)).as_bytes());
175+
176+
logger.write(format!("task finished: {}", time_task_ended));
177+
logger.write(format!("duration: {}...\n", format_duration(duration)));
159178

160179
// Log the exit code and the standard streams
161-
logfile.write_all(format!("exit code: {}.\n", exit_code).as_bytes());
162-
logfile.write_all(b"\n==stdout==\n");
163-
logfile.write_all(&output.stdout);
164-
logfile.write_all(b"\n==stderr==\n");
165-
logfile.write_all(&output.stderr);
180+
logger.write(format!("exit code: {}", exit_code));
181+
logger.write("\n==stdout==");
182+
logger.write(String::from_utf8_lossy(&output.stdout));
183+
logger.write("\n==stderr==");
184+
logger.write(String::from_utf8_lossy(&output.stderr));
166185
}
167186
}
168187

0 commit comments

Comments
 (0)