Radish alpha
r
rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE
Radicle CI adapter for native CI
Radicle
Git
refactor: add higher level API to LogFile
Lars Wirzenius committed 2 years ago
commit 4ed550de3d3a8a603f2bd77d829c1c06b18b7e74
parent 5c63697
3 files changed +43 -22
modified src/bin/radicle-native-ci.rs
@@ -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))?;

modified src/logfile.rs
@@ -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)
    }
modified src/runcmd.rs
@@ -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:?}")]