refactor: add higher level API to LogFile

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2024-01-10 17:38:05 +02:00
parent 5c6369731a
commit 4ed550de3d
3 changed files with 43 additions and 22 deletions
+1 -1
View File
@@ -213,7 +213,7 @@ impl<'a> Runner<'a> {
fn run(&mut self) -> Result<(), NativeError> {
self.log(format!("CI run on {}, {}\n", self.repo, self.commit))?;
self.run_log_str("# Log from Radicle native CI\n\n")?;
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))?;
+38
View File
@@ -23,6 +23,44 @@ impl LogFile {
})
}
pub fn h1(&mut self, text: &str) -> Result<(), LogError> {
self.write_str("# ")?;
self.write_str(text)?;
self.write_str("\n\n")?;
Ok(())
}
pub fn h2(&mut self, text: &str) -> Result<(), LogError> {
self.write_str("## ")?;
self.write_str(text)?;
self.write_str("\n\n")?;
Ok(())
}
pub fn h3(&mut self, text: &str) -> Result<(), LogError> {
self.write_str("### ")?;
self.write_str(text)?;
self.write_str("\n\n")?;
Ok(())
}
pub fn fenced(&mut self, msg: &str, data: &[u8]) -> Result<(), LogError> {
const FENCED_BLOCK: &str = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
self.h3(msg)?;
self.write_str(FENCED_BLOCK)?;
if !data.is_empty() {
let text = String::from_utf8_lossy(data);
self.write_str(&text)?;
if !text.ends_with('\n') {
self.write_str("\n")?;
}
}
self.write_str(FENCED_BLOCK)?;
self.write_str("\n")?;
Ok(())
}
pub fn write(&mut self, msg: String) -> Result<(), LogError> {
self.write_str(&msg)
}
+4 -21
View File
@@ -11,7 +11,7 @@ pub fn runcmd(log: &mut LogFile, argv: &[&str], cwd: &Path) -> Result<(), RunCmd
debug!("runcmd: argv={:?}", argv);
debug!("runcmd: cwd={:?}", cwd);
log.write_str("## Run command\n\n")?;
log.h2("Run command")?;
log.write(format!("~~~\n{:?}\n~~~\n\n", argv))?;
log.write(format!("in directory: {}\n\n", cwd.display()))?;
@@ -24,13 +24,13 @@ pub fn runcmd(log: &mut LogFile, argv: &[&str], cwd: &Path) -> Result<(), RunCmd
.map_err(|e| RunCmdError::Command(argv.iter().map(|s| s.to_string()).collect(), e))?;
debug!("{:#?}", output);
fenced(log, "Standard output:", &output.stdout)?;
fenced(log, "Standard error:", &output.stderr)?;
let exit = output.status;
debug!("exit: {:?}", exit);
log.write(format!("Exit: {}\n\n\n", exit.code().unwrap()))?;
log.fenced("Standard output", &output.stdout)?;
log.fenced("Standard error", &output.stderr)?;
if !exit.success() {
let error = Response::error(&format!("command failed: {:?}", argv));
error
@@ -44,23 +44,6 @@ pub fn runcmd(log: &mut LogFile, argv: &[&str], cwd: &Path) -> Result<(), RunCmd
Ok(())
}
fn fenced(log: &mut LogFile, msg: &str, data: &[u8]) -> Result<(), RunCmdError> {
const FENCED_BLOCK: &str = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
log.write_str(msg)?;
log.write_str("\n")?;
log.write_str(FENCED_BLOCK)?;
if !data.is_empty() {
let text = String::from_utf8_lossy(data);
log.write_str(&text)?;
if !text.ends_with('\n') {
log.write_str("\n")?;
}
}
log.write_str(FENCED_BLOCK)?;
log.write_str("\n")?;
Ok(())
}
#[derive(Debug, thiserror::Error)]
pub enum RunCmdError {
#[error("failed to write response to stdout: {0:?}")]