release_kit/commands/
doctor.rs1use serde::Serialize;
9
10use crate::cli::doctor::DoctorArgs;
11use crate::error::RkError;
12use crate::output::Output;
13use crate::probes::{self, ProbeClass, ProbeResult, ProbeStatus};
14
15#[derive(Debug, Serialize)]
17struct Report {
18 schema: &'static str,
20 probes: Vec<ProbeResult>,
22 next: Vec<String>,
24}
25
26pub fn run(args: &DoctorArgs) -> Result<(), RkError> {
33 let out = Output::new(args.json);
34 let probes = probes::run_all();
35 let next = vec!["rk usage lists every verb and flag in one call".to_owned()];
36
37 for class in [ProbeClass::Hard, ProbeClass::Soft] {
38 out.result_line(match class {
39 ProbeClass::Hard => "hard",
40 ProbeClass::Soft => "soft",
41 });
42 for probe in probes.iter().filter(|probe| probe.class == class) {
43 let status = match probe.status {
44 ProbeStatus::Ok => "ok ",
45 ProbeStatus::Failed => "failed",
46 };
47 out.result_line(format!(" {status} {}: {}", probe.id, probe.message));
48 if let Some(remediation) = &probe.remediation {
49 out.result_line(format!(" next: {remediation}"));
50 }
51 }
52 }
53 out.next(&next);
54
55 out.emit(&Report {
56 schema: "rk.doctor/1",
57 probes,
58 next,
59 })
60}
61
62#[cfg(test)]
63mod tests {
64 use crate::probes::{ProbeClass, ProbeResult, ProbeStatus};
65
66 #[test]
68 fn the_doctor_report_schema_snapshot_holds() {
69 let report = super::Report {
70 schema: "rk.doctor/1",
71 probes: vec![ProbeResult {
72 id: "sh",
73 class: ProbeClass::Hard,
74 status: ProbeStatus::Ok,
75 message: "sh runs".into(),
76 remediation: None,
77 }],
78 next: vec!["rk usage lists every verb and flag in one call".into()],
79 };
80 assert_eq!(
81 serde_json::to_string(&report).expect("a report serializes"),
82 r#"{"schema":"rk.doctor/1","probes":[{"id":"sh","class":"hard","status":"ok","message":"sh runs"}],"next":["rk usage lists every verb and flag in one call"]}"#
83 );
84 }
85
86 #[test]
88 fn the_probe_schema_snapshot_holds() {
89 let ok = ProbeResult {
90 id: "sh",
91 class: ProbeClass::Hard,
92 status: ProbeStatus::Ok,
93 message: "sh runs".into(),
94 remediation: None,
95 };
96 assert_eq!(
97 serde_json::to_string(&ok).expect("a probe serializes"),
98 r#"{"id":"sh","class":"hard","status":"ok","message":"sh runs"}"#
99 );
100 let failed = ProbeResult {
101 id: "gh-auth",
102 class: ProbeClass::Soft,
103 status: ProbeStatus::Failed,
104 message: "gh is not authenticated".into(),
105 remediation: Some("run gh auth login".into()),
106 };
107 assert_eq!(
108 serde_json::to_string(&failed).expect("a probe serializes"),
109 r#"{"id":"gh-auth","class":"soft","status":"failed","message":"gh is not authenticated","remediation":"run gh auth login"}"#
110 );
111 }
112}