diff --git a/src/bin/rad-job.rs b/src/bin/rad-job.rs index 2079ba9..ac45c0b 100644 --- a/src/bin/rad-job.rs +++ b/src/bin/rad-job.rs @@ -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, ) -> 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) -> 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, } diff --git a/src/display.rs b/src/display.rs index b88329a..985d92a 100644 --- a/src/display.rs +++ b/src/display.rs @@ -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)]