From 31e39d5a9bf57aee75cbe9a82c9940de945f00fd Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 5 Jan 2024 18:34:29 +0200 Subject: [PATCH] refactor: open run log file when creating Runner Signed-off-by: Lars Wirzenius --- src/bin/radicle-native-ci.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/bin/radicle-native-ci.rs b/src/bin/radicle-native-ci.rs index b0cd433..e9e78b2 100644 --- a/src/bin/radicle-native-ci.rs +++ b/src/bin/radicle-native-ci.rs @@ -157,21 +157,21 @@ struct Runner<'a> { commit: Oid, src: PathBuf, log: &'a mut LogFile, - run_log: PathBuf, + run_log: LogFile, builder: &'a mut RunInfoBuilder, } impl<'a> Runner<'a> { /// Perform the CI run. fn run(&mut self) -> Result<(), NativeError> { - let mut run_log = LogFile::open(&self.run_log)?; - self.log .write(format!("CI run on {}, {}\n", self.repo, self.commit))?; - run_log.write_str("# Log from Radicle native CI\n\n")?; - run_log.write(format!("* Repository id: `{}`\n", self.repo))?; - run_log.write(format!("* Commit: `{}`\n\n", 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))?; write_response(&Response::triggered(RunId::from( self.run_id.to_string().as_str(), @@ -183,7 +183,7 @@ impl<'a> Runner<'a> { debug!("cloning repository to {}", self.src.display()); self.log.write_str("clone repository\n")?; runcmd( - &mut run_log, + &mut self.run_log, &[ "git", "clone", @@ -196,7 +196,7 @@ impl<'a> Runner<'a> { debug!("checking out commit {}", self.commit); self.log.write_str("check out commit\n")?; runcmd( - &mut run_log, + &mut self.run_log, &["git", "checkout", &self.commit.to_string()], &self.src, )?; @@ -207,13 +207,13 @@ impl<'a> Runner<'a> { debug!("running CI in cloned repository"); self.log.write_str("run shell snippet in repository\n")?; let snippet = format!("set -xeuo pipefail\n{}", &runspec.shell); - runcmd(&mut run_log, &["bash", "-c", &snippet], &self.src)?; + runcmd(&mut self.run_log, &["bash", "-c", &snippet], &self.src)?; let result = RunResult::Success; write_response(&Response::finished(result.clone()))?; - run_log.write_str("CI run finished successfully\n")?; + self.run_log.write_str("CI run finished successfully\n")?; std::fs::remove_dir_all(&self.src) .map_err(|e| NativeError::RemoveDir(self.src.clone(), e))?; @@ -278,6 +278,8 @@ impl<'a> RunnerBuilder<'a> { } fn build(self) -> Result, NativeError> { + let run_log = self.run_log.ok_or(NativeError::Unset("run_log"))?; + let run_log = LogFile::open(&run_log)?; Ok(Runner { run_id: self.run_id.ok_or(NativeError::Unset("run_id"))?, storage: self.storage.ok_or(NativeError::Unset("storage"))?, @@ -285,7 +287,7 @@ impl<'a> RunnerBuilder<'a> { commit: self.commit.ok_or(NativeError::Unset("commit"))?, src: self.src.ok_or(NativeError::Unset("src"))?, log: self.log.ok_or(NativeError::Unset("log"))?, - run_log: self.run_log.ok_or(NativeError::Unset("run_log"))?, + run_log, builder: self.builder.ok_or(NativeError::Unset("builder"))?, }) }