1use std::process::ExitCode;
2
3use anyhow::Result;
4use clap::{CommandFactory, FromArgMatches};
5use tracing_subscriber::EnvFilter;
6
7pub mod claim;
8pub mod cli;
9pub mod config;
10pub mod context;
11pub mod debt;
12pub mod enforcement;
13pub mod gate;
14pub mod hooks;
15pub mod ledger;
16pub mod memory_skill;
17mod messages;
18pub mod provenance;
19pub mod reinject;
20pub mod resolve;
21pub mod reviewer;
22mod shell;
23pub mod skills;
24pub mod status;
25pub mod surface;
26mod time;
27pub mod watcher;
28
29pub fn init_tracing() {
30 let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));
31 let _ = tracing_subscriber::fmt().with_env_filter(filter).try_init();
32}
33
34pub fn run_from_env(command_name: &'static str) -> Result<ExitCode> {
35 init_tracing();
36 let matches = cli::Cli::command()
37 .name(command_name)
38 .bin_name(command_name)
39 .get_matches();
40 run(cli::Cli::from_arg_matches(&matches)?)
41}
42
43pub fn run(cli: cli::Cli) -> Result<ExitCode> {
44 let cli::Cli {
45 state_dir,
46 config: config_path,
47 command,
48 } = cli;
49
50 let command = match command {
51 cli::Commands::Status(args) => return status::run(args, &state_dir),
52 command => command,
53 };
54
55 let config = config::TruthMirrorConfig::load_for_cli(config_path.as_deref(), &state_dir)?;
56
57 match command {
58 cli::Commands::InstallHooks(args) => {
59 hooks::run(args, &state_dir, config_path.as_deref(), &config)
60 }
61 cli::Commands::Review(args) => reviewer::run_review_command(args, &state_dir, &config),
62 cli::Commands::Gate(args) => gate::run(args, &state_dir, config_path.as_deref(), &config),
63 cli::Commands::Reinject(args) => reinject::run(args, &state_dir, &config),
64 cli::Commands::Ledger(args) => ledger::run(args, &state_dir),
65 cli::Commands::Resolve(args) => resolve::run(args, &state_dir, &config),
66 cli::Commands::Debt(args) => debt::run(args, &state_dir),
67 cli::Commands::MemorySkill(args) => memory_skill::run(args, &state_dir, &config),
68 cli::Commands::Watch(args) => reviewer::run_watch_command(args, &state_dir, &config),
69 cli::Commands::EnsureWatcher(args) => {
70 reviewer::run_ensure_watcher_command(args, &state_dir)
71 }
72 cli::Commands::Status(_) => unreachable!("status returns before config load"),
73 cli::Commands::Skills(args) => skills::run(args),
74 cli::Commands::Uninstall(args) => {
75 hooks::run_uninstall(args, &state_dir, config_path.as_deref())
76 }
77 cli::Commands::HookDispatch(args) => {
78 hooks::dispatch(args, &state_dir, config_path.as_deref(), &config)
79 }
80 }
81}