Radish alpha
r
rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE
Radicle CI adapter for native CI
Radicle
Git
add timestamps for every commancd run
Lars Wirzenius committed 2 years ago
commit dd7496fa70d751ef61ca695d64a1a63438425322
parent 89f0b97
3 files changed +55 -3
modified src/bin/run_log.rs
@@ -1,4 +1,4 @@
-
use std::path::Path;
+
use std::{path::Path, time::SystemTime};

use radicle_ci_broker::msg::{Oid, RepoId};

@@ -21,12 +21,16 @@ fn main() {
    run_log.runspec(RunSpec {
        shell: "echo hello, world".into(),
    });
+
    let started = SystemTime::now();
+
    let ended = SystemTime::now();
    run_log.runcmd(
        &["git", "pull"],
        Path::new("/tmp"),
        0,
        "This is stdout".as_bytes(),
        "Error messages go here".as_bytes(),
+
        started,
+
        ended,
    );
    run_log.runcmd(
        &["cargo", "check", "--all-targets", "--workspace"],
@@ -34,6 +38,8 @@ fn main() {
        0,
        "This is stdout".as_bytes(),
        "Error messages go here".as_bytes(),
+
        started,
+
        ended,
    );

    run_log.write().expect("write html log");
modified src/run.rs
@@ -1,6 +1,7 @@
use std::{
    path::{Path, PathBuf},
    process::Command,
+
    time::SystemTime,
};

use radicle_ci_broker::msg::{Oid, RepoId};
@@ -196,12 +197,14 @@ impl Run {
            return Err(RunError::EmptyArgv);
        }

+
        let started = SystemTime::now();
        let argv0 = argv[0];
        let output = Command::new(argv0)
            .args(&argv[1..])
            .current_dir(cwd)
            .output()
            .map_err(|e| RunError::Command(argv.iter().map(|s| s.to_string()).collect(), e))?;
+
        let ended = SystemTime::now();

        let exit = output.status.code().unwrap_or(NO_EXIT);
        self.run_log.runcmd(
@@ -211,6 +214,8 @@ impl Run {
            exit,
            &output.stdout,
            &output.stderr,
+
            started,
+
            ended,
        );
        Ok(exit)
    }
modified src/runlog.rs
@@ -1,6 +1,10 @@
-
use std::path::{Path, PathBuf};
+
use std::{
+
    path::{Path, PathBuf},
+
    time::{Duration, SystemTime},
+
};

use html_page::{Document, Element, Tag};
+
use time::{macros::format_description, OffsetDateTime};

use radicle_ci_broker::msg::{Oid, RepoId};

@@ -65,8 +69,22 @@ impl RunLog {
        self.runspec_error = Some(format!("{}", e));
    }

-
    pub fn runcmd(&mut self, argv: &[&str], cwd: &Path, exit: i32, stdout: &[u8], stderr: &[u8]) {
+
    // FIXME: refactor this to maybe use a builder pattern?
+
    #[allow(clippy::too_many_arguments)]
+
    pub fn runcmd(
+
        &mut self,
+
        argv: &[&str],
+
        cwd: &Path,
+
        exit: i32,
+
        stdout: &[u8],
+
        stderr: &[u8],
+
        started: SystemTime,
+
        ended: SystemTime,
+
    ) {
        self.commands.push(Command {
+
            started,
+
            ended,
+
            duration: ended.duration_since(started).expect("compute duration"),
            argv: argv.iter().map(|a| a.to_string()).collect(),
            cwd: cwd.into(),
            exit,
@@ -182,6 +200,9 @@ impl RunLog {

#[derive(Debug)]
struct Command {
+
    started: SystemTime,
+
    ended: SystemTime,
+
    duration: Duration,
    argv: Vec<String>,
    cwd: PathBuf,
    exit: i32,
@@ -203,6 +224,21 @@ impl Command {
            ),
        );

+
        body.push_child(
+
            &Element::new(Tag::Ul)
+
                .with_child(
+
                    Element::new(Tag::Li)
+
                        .with_text(&format!("Started: {}", timestamp(&self.started))),
+
                )
+
                .with_child(
+
                    Element::new(Tag::Li).with_text(&format!("Ended: {}", timestamp(&self.ended))),
+
                )
+
                .with_child(
+
                    Element::new(Tag::Li)
+
                        .with_text(&format!("Duration: {} s", self.duration.as_secs())),
+
                ),
+
        );
+

        body.push_child(&Element::new(Tag::P).with_text("Command arguments:"));
        let mut ul = Element::new(Tag::Ul);
        for arg in self.argv.iter() {
@@ -236,6 +272,11 @@ impl Command {
    }
}

+
fn timestamp(when: &SystemTime) -> String {
+
    let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]Z");
+
    OffsetDateTime::from(*when).format(fmt).ok().unwrap()
+
}
+

#[derive(Debug, Default)]
struct ToC {
    counter: usize,