feat: add --pretty to 'rad-job show'

This involves moving pretty-printing of individual jobs from
display::Jobs to display::Job.

Signed-off-by: Richard Levitte <richard@levitte.org>
This commit is contained in:
Richard Levitte
2025-07-18 10:35:40 +01:00
committed by Fintan Halpenny
parent 841ea446c0
commit 8530300d04
2 changed files with 37 additions and 19 deletions
+15 -8
View File
@@ -37,7 +37,7 @@ fn main() {
}
}
fn fallible_main() -> Result<(), Error> {
fn fallible_main() -> Result<(), RadJobError> {
let args = Args::parse();
run(args)
}
@@ -126,7 +126,7 @@ fn announce(profile: &Profile, repo_id: RepoId) -> Result<(), error::Announce> {
Ok(())
}
fn run(args: Args) -> Result<(), Error> {
fn run(args: Args) -> Result<(), RadJobError> {
use command::*;
let profile = profile()?;
@@ -259,15 +259,19 @@ where
}
fn show_job(
command::Show { oid }: command::Show,
command::Show { pretty, oid }: command::Show,
jobs: &Jobs<Repository>,
) -> Result<(), error::Show> {
let (id, job) = find_by_commit(oid, jobs)?;
let show = radicle_job::display::Job::new(id, &job);
println!(
"{}",
serde_json::to_string_pretty(&show).map_err(error::Show::Json)?
);
if pretty {
println!("{}", show.pretty());
} else {
println!(
"{}",
serde_json::to_string_pretty(&show).map_err(error::Show::Json)?
);
}
Ok(())
}
@@ -280,7 +284,7 @@ fn find_by_commit(oid: Oid, jobs: &Jobs<Repository>) -> Result<(JobId, Job), err
}
#[derive(Debug, thiserror::Error)]
enum Error {
enum RadJobError {
#[error(transparent)]
Profile(#[from] error::Profile),
#[error(transparent)]
@@ -345,6 +349,9 @@ mod command {
/// Show the job COB for a Git commit.
#[derive(Parser)]
pub struct Show {
/// Format output in a more human oriented way than JSON.
#[clap(long)]
pub pretty: bool,
/// Git object for the commit.
pub oid: Oid,
}
+22 -11
View File
@@ -44,17 +44,7 @@ impl Jobs {
line(&mut s, format!("count: {}", self.count));
for shown in self.jobs.iter() {
line(
&mut s,
format!("job {} (commit {})", shown.job_id, shown.oid),
);
for run in shown.runs.iter() {
line(&mut s, format!(" node {}", run.node_id));
for run2 in run.runs.iter() {
line(&mut s, format!(" run {} {:?}", run2.run_id, run2.status));
line(&mut s, format!(" log {}", run2.log));
}
}
s.push_str(&shown.pretty());
s.push('\n');
}
@@ -105,6 +95,27 @@ impl Job {
runs,
}
}
/// Pretty print the set of [`Jobs`] and their count.
pub fn pretty(&self) -> String {
fn line(s: &mut String, line: String) {
s.push_str(&line);
s.push('\n');
}
let mut s = String::new();
line(&mut s, format!("job {} (commit {})", self.job_id, self.oid));
for run in self.runs.iter() {
line(&mut s, format!(" node {}", run.node_id));
for run2 in run.runs.iter() {
line(&mut s, format!(" run {} {:?}", run2.run_id, run2.status));
line(&mut s, format!(" log {}", run2.log));
}
}
s
}
}
#[derive(Serialize)]