From 8e9b908b07bca31c19d0a947207099b2785fbeb0 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 10 Jan 2024 14:27:12 +0200 Subject: [PATCH] reactor: add methods to write lines Signed-off-by: Lars Wirzenius --- src/bin/radicle-native-ci.rs | 39 ++++++++++-------------------------- src/logfile.rs | 17 ++++++++++++++++ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/bin/radicle-native-ci.rs b/src/bin/radicle-native-ci.rs index da6b26f..dc6f485 100644 --- a/src/bin/radicle-native-ci.rs +++ b/src/bin/radicle-native-ci.rs @@ -162,29 +162,9 @@ 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(()) - } - fn git_clone(&mut self, repo_path: &Path) -> Result<(), NativeError> { debug!("cloning repository to {}", self.src.display()); - self.log_str("clone repository\n")?; + self.log.writeln_str("clone repository")?; runcmd( &mut self.run_log, &[ @@ -200,7 +180,7 @@ impl<'a> Runner<'a> { fn git_checkout(&mut self) -> Result<(), NativeError> { debug!("checking out commit {}", self.commit); - self.log_str("check out commit\n")?; + self.log.writeln_str("check out commit")?; runcmd( &mut self.run_log, &["git", "checkout", &self.commit.to_string()], @@ -211,11 +191,14 @@ impl<'a> Runner<'a> { /// Perform the CI run. fn run(&mut self) -> Result<(), NativeError> { - self.log(format!("CI run on {}, {}\n", self.repo, self.commit))?; + self.log + .writeln(format!("CI run on {}, {}", self.repo, self.commit))?; self.run_log.h1("Log from Radicle native CI")?; - self.run_log(format!("* Repository id: `{}`\n", self.repo))?; - self.run_log(format!("* Commit: `{}`\n\n", self.commit))?; + self.run_log + .bullet_point(format!("* Repository id: `{}`", self.repo))?; + self.run_log + .bullet_point(format!("* Commit: `{}", self.commit))?; write_triggered(&self.run_id)?; @@ -226,10 +209,10 @@ impl<'a> Runner<'a> { self.git_checkout()?; let runspec = RunSpec::from_file(&self.src.join(RUNSPEC_PATH))?; - self.log(format!("CI run spec: {:#?}\n", runspec))?; + self.log.writeln(format!("CI run spec: {:#?}", runspec))?; debug!("running CI in cloned repository"); - self.log_str("run shell snippet in repository\n")?; + self.log.writeln_str("run shell snippet in repository")?; let snippet = format!("set -xeuo pipefail\n{}", &runspec.shell); runcmd(&mut self.run_log, &["bash", "-c", &snippet], &self.src)?; @@ -237,7 +220,7 @@ impl<'a> Runner<'a> { write_response(&Response::finished(result.clone()))?; - self.run_log_str("CI run finished successfully\n")?; + self.run_log.writeln_str("CI run finished successfully")?; std::fs::remove_dir_all(&self.src) .map_err(|e| NativeError::RemoveDir(self.src.clone(), e))?; diff --git a/src/logfile.rs b/src/logfile.rs index 83911ee..a559dc2 100644 --- a/src/logfile.rs +++ b/src/logfile.rs @@ -44,6 +44,23 @@ impl LogFile { Ok(()) } + pub fn bullet_point(&mut self, text: String) -> Result<(), LogError> { + self.write(text)?; + self.write_str("\n")?; + Ok(()) + } + + pub fn writeln(&mut self, text: String) -> Result<(), LogError> { + self.writeln_str(&text)?; + Ok(()) + } + + pub fn writeln_str(&mut self, text: &str) -> Result<(), LogError> { + self.write_str(text)?; + self.write_str("\n")?; + Ok(()) + } + pub fn fenced(&mut self, msg: &str, data: &[u8]) -> Result<(), LogError> { const FENCED_BLOCK: &str = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; self.h3(msg)?;