Skip to main content

vitrify_cli/
lib.rs

1//! vitrify library target: exposes the CLI internals so integration
2//! tests can exercise the git layer and command modules directly. The
3//! `vitrify` binary is a thin wrapper over [`run`].
4
5pub mod cli;
6pub mod cmd;
7pub mod exit;
8pub mod gate;
9pub mod git;
10pub mod help;
11pub mod home;
12pub mod layout;
13pub mod out;
14pub mod run;
15pub mod signals;
16
17use clap::Parser;
18use vitrify_core::CoreError;
19
20use crate::cli::{Cli, Cmd};
21
22/// Parse argv and dispatch; returns the exit code.
23pub fn run() -> u8 {
24    let cli = Cli::parse();
25    match dispatch(cli) {
26        Ok(code) => code,
27        Err(err) => {
28            out::diag(&err.to_string());
29            err.exit_code()
30        }
31    }
32}
33
34fn dispatch(cli: Cli) -> Result<u8, CoreError> {
35    // Every command but `init` needs the home `init` sets up, and refuses —
36    // the same way, naming `vitrify init` — on a machine without one. Each
37    // reads the configuration the same way too, so a file that does not
38    // parse, a value of the wrong type, a schema this vitrify does not read,
39    // or a repository's config.toml carrying the probe record stops every
40    // command alike, whether or not it reads a setting.
41    if !matches!(cli.cmd, Cmd::Init { .. }) {
42        let Some(home_dir) = cmd::home_dir() else {
43            return Err(CoreError::refusal(
44                1,
45                "error: cannot resolve the vitrify home (set VITRIFY_HOME or HOME)",
46            ));
47        };
48        home::require_set_up(&home_dir)?;
49        cmd::load_config(&vitrify_core::config::Overrides::default())?;
50    }
51    match cli.cmd {
52        Cmd::Locate { units, sdd, json } => cmd::locate::run(units, sdd.as_deref(), json),
53        Cmd::Readset {
54            unit,
55            sdd,
56            json,
57            budget,
58        } => cmd::readset::run(&unit, sdd.as_deref(), json, budget),
59        Cmd::Claim(args) => cmd::claim::run(vitrify_core::claims::Op::Claim, &args),
60        Cmd::Renew(args) => cmd::claim::run(vitrify_core::claims::Op::Renew, &args),
61        Cmd::Release(args) => cmd::claim::run(vitrify_core::claims::Op::Release, &args),
62        Cmd::Trace { mode } => match mode {
63            cli::TraceMode::Record {
64                unit,
65                stamp,
66                commit,
67                sdd,
68            } => cmd::trace::record(&unit, stamp, commit.as_deref(), sdd.as_deref()),
69            cli::TraceMode::Verify { unit, sdd, json } => {
70                cmd::trace::verify(&unit, sdd.as_deref(), json)
71            }
72        },
73        Cmd::Ledger { mode } => match mode {
74            cli::LedgerMode::Regen { sdd, all, check } => {
75                cmd::ledger::regen(sdd.as_deref(), all, check)
76            }
77            cli::LedgerMode::Progress { sdd, all } => cmd::ledger::progress(sdd.as_deref(), all),
78        },
79        Cmd::Journal { mode } => match mode {
80            cli::JournalMode::Append {
81                event,
82                unit,
83                actor,
84                claim_seq,
85                entry,
86                stale,
87                cause,
88                findings,
89                commit,
90                sdd,
91            } => cmd::journal::append(
92                event,
93                cmd::journal::AppendArgs {
94                    unit,
95                    actor,
96                    claim_seq,
97                    entry,
98                    stale,
99                    cause,
100                    findings,
101                    commit,
102                    sdd,
103                },
104            ),
105            cli::JournalMode::Replay { json, sdd, all } => {
106                cmd::journal::replay(json, sdd.as_deref(), all)
107            }
108        },
109        Cmd::Audit {
110            json,
111            fix_leases,
112            apply_stale,
113            skip_rotten,
114            journal,
115            sdd,
116            all,
117        } => cmd::audit::run(cmd::audit::Flags {
118            json,
119            fix_leases,
120            apply_stale,
121            skip_rotten,
122            journal,
123            sdd,
124            all,
125        }),
126        Cmd::Brief {
127            phase,
128            unit,
129            holder,
130            sdd,
131        } => cmd::brief::run(phase, unit.as_deref(), holder.as_deref(), sdd.as_deref()),
132        Cmd::New {
133            kind,
134            bundle: _,
135            single,
136            name,
137        } => cmd::new::run(kind, single, name.as_deref()),
138        Cmd::Schema { mode } => match mode {
139            cli::SchemaMode::Show { tdd } => cmd::schema::show(tdd),
140        },
141        Cmd::Init { json } => cmd::init::run(json),
142        Cmd::Probe { json, assume_yes } => cmd::probe::run(json, assume_yes),
143        Cmd::Lint {
144            sdd,
145            all,
146            built,
147            json,
148        } => cmd::lint::run(cmd::lint::Flags {
149            sdd,
150            all,
151            built,
152            json,
153        }),
154        Cmd::Check {
155            sdd,
156            all,
157            skip_rotten,
158            json,
159        } => cmd::check::run(cmd::check::Flags {
160            sdd,
161            all,
162            skip_rotten,
163            json,
164        }),
165        Cmd::Change { mode } => match mode {
166            cli::ChangeMode::Apply {
167                sdd,
168                all,
169                dry_run,
170                commit,
171                json,
172            } => cmd::change::apply(cmd::change::ApplyFlags {
173                sdd,
174                all,
175                dry_run,
176                commit,
177                json,
178            }),
179        },
180        Cmd::Pin { mode } => match mode {
181            cli::PinMode::List { sdd, all, json } => cmd::pin::list(sdd.as_deref(), all, json),
182            cli::PinMode::Sync {
183                sdd,
184                all,
185                dry_run,
186                json,
187            } => cmd::pin::sync(sdd.as_deref(), all, dry_run, json),
188        },
189        Cmd::Questions {
190            sdd,
191            all,
192            open,
193            json,
194        } => cmd::questions::run(sdd.as_deref(), all, open, json),
195        Cmd::Run {
196            sub: Some(cli::RunSub::Status(args)),
197            ..
198        } => cmd::run_status::run(&args),
199        Cmd::Run {
200            dry_run,
201            cap,
202            gate_jobs,
203            wait,
204            sdd,
205            mainline,
206            sub: None,
207        } => cmd::run::run(run::RunFlags {
208            dry_run,
209            cap,
210            sdd,
211            mainline,
212            gate_jobs,
213            wait,
214        }),
215    }
216}