1use vitrify_core::CoreError;
12use vitrify_core::content;
13
14use crate::home;
15use crate::out::{J, emit};
16
17use super::probe::{self, CONFIG_LABEL, CheckLine};
18
19pub fn run(json: bool) -> Result<u8, CoreError> {
20 let Some(home_dir) = super::home_dir() else {
21 return Err(CoreError::refusal(
22 1,
23 "error: cannot resolve the vitrify home (set VITRIFY_HOME or HOME)",
24 ));
25 };
26 let path = home::config_path(&home_dir);
27 if path.exists() {
28 return Err(CoreError::refusal(
29 2,
30 format!(
31 "error: refusing to overwrite existing {CONFIG_LABEL} → vitrify probe brings its probe record up to date"
32 ),
33 ));
34 }
35 let version = super::brief::VERSION;
36
37 std::fs::create_dir_all(&home_dir).map_err(|source| CoreError::Io {
38 path: home_dir.clone(),
39 source,
40 })?;
41 probe::write_config(&home_dir, content::CONFIG.body)?;
44
45 let mut checks = vec![
46 CheckLine::new("ok", format!("vitrify version {version}")),
47 CheckLine::new(
48 "ok",
49 format!("created {CONFIG_LABEL} with every setting at its default"),
50 ),
51 ];
52 if home_dir.join("harness.toml").exists() {
53 checks.push(CheckLine::new(
54 "note",
55 format!(
56 "~/.vitrify/harness.toml is not read: the role profiles are the [harness.<name>] tables of {CONFIG_LABEL}"
57 ),
58 ));
59 }
60
61 let probed = probe::probe_machine(&home_dir, version, content::CONFIG.body)?;
62 let failed = probed.failed();
63 let text = probe::updated_text(
64 content::CONFIG.body,
65 &probe::render_record(version, &probed),
66 )?;
67 probe::write_config(&home_dir, &text)?;
68 checks.extend(
69 probed
70 .checks
71 .iter()
72 .map(|c| CheckLine::new(c.status, c.text.clone())),
73 );
74 checks.push(CheckLine::new(
75 "ok",
76 format!("recorded the probe in {CONFIG_LABEL}"),
77 ));
78
79 if json {
80 let obj = probe::json_of(&checks, &probed, failed);
81 emit(&J::Obj(obj).dumps_indent2());
82 } else {
83 for c in &checks {
84 emit(&format!("{}: {}", c.status, c.text));
85 }
86 }
87 Ok(if failed { 2 } else { 0 })
88}