mirror of
https://seed.radicle.dev/rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE.git
synced 2026-11-29 14:30:32 +08:00
refactor: add higher level API to LogFile
Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
@@ -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))?;
|
||||
|
||||
|
||||
@@ -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
@@ -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:?}")]
|
||||
|
||||
Reference in New Issue
Block a user