refactor: add helper functions to log

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2024-01-10 17:38:05 +02:00
parent 31e39d5a9b
commit a0aee4df4e
+29 -12
View File
@@ -162,16 +162,33 @@ struct Runner<'a> {
}
impl<'a> Runner<'a> {
fn run_log_str(&mut self, msg: &str) -> Result<(), NativeError> {
self.run_log.write_str(msg)?;
Ok(())
}
fn run_log(&mut self, msg: String) -> Result<(), NativeError> {
self.run_log.write(msg)?;
Ok(())
}
fn log_str(&mut self, msg: &str) -> Result<(), NativeError> {
self.log.write_str(msg)?;
Ok(())
}
fn log(&mut self, msg: String) -> Result<(), NativeError> {
self.log.write(msg)?;
Ok(())
}
/// Perform the CI run.
fn run(&mut self) -> Result<(), NativeError> {
self.log
.write(format!("CI run on {}, {}\n", self.repo, self.commit))?;
self.log(format!("CI run on {}, {}\n", self.repo, self.commit))?;
self.run_log.write_str("# Log from Radicle native CI\n\n")?;
self.run_log
.write(format!("* Repository id: `{}`\n", self.repo))?;
self.run_log
.write(format!("* Commit: `{}`\n\n", self.commit))?;
self.run_log_str("# Log from Radicle native CI\n\n")?;
self.run_log(format!("* Repository id: `{}`\n", self.repo))?;
self.run_log(format!("* Commit: `{}`\n\n", self.commit))?;
write_response(&Response::triggered(RunId::from(
self.run_id.to_string().as_str(),
@@ -181,7 +198,7 @@ impl<'a> Runner<'a> {
debug!("repo path: {}", repo_path.display());
debug!("cloning repository to {}", self.src.display());
self.log.write_str("clone repository\n")?;
self.log_str("clone repository\n")?;
runcmd(
&mut self.run_log,
&[
@@ -194,7 +211,7 @@ impl<'a> Runner<'a> {
)?;
debug!("checking out commit {}", self.commit);
self.log.write_str("check out commit\n")?;
self.log_str("check out commit\n")?;
runcmd(
&mut self.run_log,
&["git", "checkout", &self.commit.to_string()],
@@ -202,10 +219,10 @@ impl<'a> Runner<'a> {
)?;
let runspec = RunSpec::from_file(&self.src.join(RUNSPEC_PATH))?;
self.log.write(format!("CI run spec: {:#?}\n", runspec))?;
self.log(format!("CI run spec: {:#?}\n", runspec))?;
debug!("running CI in cloned repository");
self.log.write_str("run shell snippet in repository\n")?;
self.log_str("run shell snippet in repository\n")?;
let snippet = format!("set -xeuo pipefail\n{}", &runspec.shell);
runcmd(&mut self.run_log, &["bash", "-c", &snippet], &self.src)?;
@@ -213,7 +230,7 @@ impl<'a> Runner<'a> {
write_response(&Response::finished(result.clone()))?;
self.run_log.write_str("CI run finished successfully\n")?;
self.run_log_str("CI run finished successfully\n")?;
std::fs::remove_dir_all(&self.src)
.map_err(|e| NativeError::RemoveDir(self.src.clone(), e))?;