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 state;
25pub mod status;
26pub mod surface;
27mod time;
28pub mod tui;
29pub mod watcher;
30
31pub fn init_tracing() {
32 let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));
33 let _ = tracing_subscriber::fmt().with_env_filter(filter).try_init();
34}
35
36pub fn run_from_env(command_name: &'static str) -> Result<ExitCode> {
37 init_tracing();
38 let matches = cli::Cli::command()
39 .name(command_name)
40 .bin_name(command_name)
41 .get_matches();
42 run(cli::Cli::from_arg_matches(&matches)?)
43}
44
45pub fn run(cli: cli::Cli) -> Result<ExitCode> {
46 let cli::Cli {
47 state_dir,
48 config: config_path,
49 command,
50 } = cli;
51
52 let command = match command {
53 cli::Commands::Status(args) => {
54 return status::run(args, &state_dir, config_path.as_deref());
55 }
56 cli::Commands::Tui(args) => {
57 return tui::run(args, &state_dir, config_path.as_deref());
58 }
59 command => command,
60 };
61
62 let config = config::TruthMirrorConfig::load_for_cli(config_path.as_deref(), &state_dir)?;
63
64 match command {
65 cli::Commands::InstallHooks(args) => {
66 hooks::run(args, &state_dir, config_path.as_deref(), &config)
67 }
68 cli::Commands::Review(args) => reviewer::run_review_command(args, &state_dir, &config),
69 cli::Commands::Gate(args) => gate::run(args, &state_dir, config_path.as_deref(), &config),
70 cli::Commands::Reinject(args) => reinject::run(args, &state_dir, &config),
71 cli::Commands::Ledger(args) => ledger::run(args, &state_dir),
72 cli::Commands::Resolve(args) => resolve::run(args, &state_dir, &config),
73 cli::Commands::Debt(args) => debt::run(args, &state_dir),
74 cli::Commands::Tui(_) => unreachable!("tui returns before config load"),
75 cli::Commands::MemorySkill(args) => memory_skill::run(args, &state_dir, &config),
76 cli::Commands::Watch(args) => reviewer::run_watch_command(args, &state_dir, &config),
77 cli::Commands::EnsureWatcher(args) => {
78 reviewer::run_ensure_watcher_command(args, &state_dir)
79 }
80 cli::Commands::Status(_) => unreachable!("status returns before config load"),
81 cli::Commands::Skills(args) => skills::run(args),
82 cli::Commands::Uninstall(args) => {
83 hooks::run_uninstall(args, &state_dir, config_path.as_deref())
84 }
85 cli::Commands::HookDispatch(args) => {
86 hooks::dispatch(args, &state_dir, config_path.as_deref(), &config)
87 }
88 }
89}