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