|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +//! sillybot entry point. See `lib.rs` and `cli.rs`. |
| 3 | +//! |
| 4 | +//! Output contract for `--format json` (what the report-back driver reads): |
| 5 | +//! - ran: a JSON array of findings (possibly empty), exit 0 (no blocking |
| 6 | +//! finding) or 1 (at least one blocking finding), |
| 7 | +//! - error: `{"status":"error","reason":"..."}`, exit 2, |
| 8 | +//! - not run: `{"status":"not_run","reason":"..."}`, exit 3. |
| 9 | +//! |
| 10 | +//! Nothing here ever prints an empty array for a scan that did not run. |
| 11 | +
|
| 12 | +use clap::Parser; |
| 13 | +use sillybot::cli::{Cli, Command, OutputFormat}; |
| 14 | +use sillybot::runner::{scan, Outcome, Request}; |
| 15 | +use sillybot::{pin, translator}; |
| 16 | +use std::process::ExitCode; |
| 17 | +use std::time::Duration; |
| 18 | + |
| 19 | +fn main() -> ExitCode { |
| 20 | + let cli = Cli::parse(); |
| 21 | + let Command::Scan { |
| 22 | + path, |
| 23 | + base, |
| 24 | + repo, |
| 25 | + pr, |
| 26 | + timeout, |
| 27 | + } = cli.command; |
| 28 | + if let Err(e) = pin::pinned() { |
| 29 | + return report_not_ran( |
| 30 | + "error", |
| 31 | + &format!("canon/pin.toml is malformed: {e}"), |
| 32 | + cli.format, |
| 33 | + 2, |
| 34 | + ); |
| 35 | + } |
| 36 | + let req = Request { |
| 37 | + path, |
| 38 | + base, |
| 39 | + repo, |
| 40 | + pr, |
| 41 | + timeout: Duration::from_secs(timeout.max(1)), |
| 42 | + }; |
| 43 | + match scan(&req) { |
| 44 | + Outcome::Ran(doc) => match translator::translate_report(&doc) { |
| 45 | + Ok(findings) => print_findings(&findings, cli.format), |
| 46 | + Err(e) => report_not_ran( |
| 47 | + "error", |
| 48 | + &format!("reconcile output is unusable: {e}"), |
| 49 | + cli.format, |
| 50 | + 2, |
| 51 | + ), |
| 52 | + }, |
| 53 | + Outcome::Error(reason) => report_not_ran("error", &reason, cli.format, 2), |
| 54 | + Outcome::NotRun(reason) => report_not_ran("not_run", &reason, cli.format, 3), |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn print_findings(findings: &[translator::Finding], format: OutputFormat) -> ExitCode { |
| 59 | + match format { |
| 60 | + OutputFormat::Json => match serde_json::to_string_pretty(findings) { |
| 61 | + Ok(s) => println!("{s}"), |
| 62 | + Err(e) => return report_not_ran("error", &e.to_string(), format, 2), |
| 63 | + }, |
| 64 | + OutputFormat::Text => { |
| 65 | + if findings.is_empty() { |
| 66 | + println!("No docs findings."); |
| 67 | + } |
| 68 | + for f in findings { |
| 69 | + println!( |
| 70 | + "{}:{} [{}] {} {}", |
| 71 | + f.file, f.line, f.rule_id, f.severity, f.message |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + if findings.iter().any(|f| f.blocking) { |
| 77 | + ExitCode::from(1) |
| 78 | + } else { |
| 79 | + ExitCode::SUCCESS |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fn report_not_ran(status: &str, reason: &str, format: OutputFormat, code: u8) -> ExitCode { |
| 84 | + match format { |
| 85 | + OutputFormat::Json => { |
| 86 | + println!( |
| 87 | + "{}", |
| 88 | + serde_json::json!({ "status": status, "reason": reason }) |
| 89 | + ) |
| 90 | + } |
| 91 | + OutputFormat::Text => println!("sillybot: {status}: {reason}"), |
| 92 | + } |
| 93 | + eprintln!("sillybot: {status}: {reason}"); |
| 94 | + ExitCode::from(code) |
| 95 | +} |
0 commit comments