Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: add `cibtool log --jsonl` option
Lars Wirzenius committed 8 months ago
commit cf188b6713dbdc718969417b8bbc31a0e8394311
parent e171baf
1 file changed +40 -9
modified src/bin/cibtoolcmd/log.rs
@@ -24,6 +24,10 @@ pub struct LogCmd {
    #[clap(long)]
    journal: Option<PathBuf>,

+
    /// One JSON object per line.
+
    #[clap(long)]
+
    jsonl: bool,
+

    /// Include messages only if they are at least of this log level.
    /// Default is to allow any log level.
    #[clap(long)]
@@ -88,6 +92,26 @@ impl LogCmd {
        }
        true
    }
+

+
    fn write_pretty(&self, mut out: impl Write, journal: JournalLines) -> Result<(), LogError> {
+
        let messages = journal.flatten().filter(|msg| self.allowed(msg));
+
        for msg in messages {
+
            out.write_all(format!("{}\n", pretty(msg)?).as_bytes())
+
                .map_err(LogError::Write)?;
+
        }
+

+
        Ok(())
+
    }
+

+
    fn write_jsonl(&self, mut out: impl Write, journal: JournalLines) -> Result<(), LogError> {
+
        let messages = journal.flatten().filter(|msg| self.allowed(msg));
+
        for msg in messages {
+
            out.write_all(format!("{}\n", jsonl(msg)?).as_bytes())
+
                .map_err(LogError::Write)?;
+
        }
+

+
        Ok(())
+
    }
}

fn log_level(msg: &Value) -> Option<LogLevel> {
@@ -116,6 +140,10 @@ fn pretty(msg: Value) -> Result<String, LogError> {
    serde_json::to_string_pretty(&msg).map_err(|err| LogError::JsonSer(msg, err))
}

+
fn jsonl(msg: Value) -> Result<String, LogError> {
+
    serde_json::to_string(&msg).map_err(|err| LogError::JsonSer(msg, err))
+
}
+

impl Leaf for LogCmd {
    fn run(&self, _args: &Args) -> Result<(), CibToolError> {
        let journal = if let Some(filename) = &self.journal {
@@ -124,17 +152,20 @@ impl Leaf for LogCmd {
            JournalLines::from_stdin()?
        };

-
        let messages = journal.flatten().filter(|msg| self.allowed(msg));
        if let Some(filename) = &self.output {
-
            let mut file = File::create(filename)
+
            let file = File::create(filename)
                .map_err(|err| LogError::CreateOutput(filename.into(), err))?;
-
            for msg in messages {
-
                file.write_all(format!("{}\n", pretty(msg)?).as_bytes())
-
                    .map_err(|err| LogError::WriteOutput(filename.into(), err))?;
+
            if self.jsonl {
+
                self.write_jsonl(file, journal)?;
+
            } else {
+
                self.write_pretty(file, journal)?;
            }
        } else {
-
            for msg in messages {
-
                println!("{}", pretty(msg)?);
+
            let out = std::io::stdout();
+
            if self.jsonl {
+
                self.write_jsonl(out, journal)?;
+
            } else {
+
                self.write_pretty(out, journal)?;
            }
        }

@@ -212,6 +243,6 @@ pub enum LogError {
    #[error("failed to create output file {0}")]
    CreateOutput(PathBuf, #[source] std::io::Error),

-
    #[error("failed to write to output file {0}")]
-
    WriteOutput(PathBuf, #[source] std::io::Error),
+
    #[error("failed to write to output")]
+
    Write(#[source] std::io::Error),
}