Radish alpha
r
rad:z2UcCU1LgMshWvXj6hXSDDrwB8q8M
Radicle Job Collaborative Object
Radicle
Git
feat: add --pretty to 'rad-job show'
Richard Levitte committed 9 months ago
commit 8530300d045448b7b7173d8a5280d18e493edbbb
parent 841ea44
2 files changed +37 -19
modified 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<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,
    }
modified 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)]