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 enforcement;
12pub mod gate;
13pub mod hooks;
14pub mod ledger;
15pub mod memory_skill;
16mod messages;
17pub mod provenance;
18pub mod reinject;
19pub mod reviewer;
20mod shell;
21pub mod skills;
22pub mod status;
23pub mod surface;
24mod time;
25
26pub fn init_tracing() {
27 let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));
28 let _ = tracing_subscriber::fmt().with_env_filter(filter).try_init();
29}
30
31pub fn run_from_env(command_name: &'static str) -> Result<ExitCode> {
32 init_tracing();
33 let matches = cli::Cli::command()
34 .name(command_name)
35 .bin_name(command_name)
36 .get_matches();
37 run(cli::Cli::from_arg_matches(&matches)?)
38}
39
40pub fn run(cli: cli::Cli) -> Result<ExitCode> {
41 let cli::Cli {
42 state_dir,
43 config: config_path,
44 command,
45 } = cli;
46
47 let command = match command {
48 cli::Commands::Status(args) => return status::run(args, &state_dir),
49 command => command,
50 };
51
52 let config = config::TruthMirrorConfig::load_for_cli(config_path.as_deref(), &state_dir)?;
53
54 match command {
55 cli::Commands::InstallHooks(args) => {
56 hooks::run(args, &state_dir, config_path.as_deref(), &config)
57 }
58 cli::Commands::Review(args) => reviewer::run_review_command(args, &state_dir, &config),
59 cli::Commands::Gate(args) => gate::run(args, &state_dir, config_path.as_deref(), &config),
60 cli::Commands::Reinject(args) => reinject::run(args, &state_dir, &config),
61 cli::Commands::Ledger(args) => ledger::run(args, &state_dir),
62 cli::Commands::MemorySkill(args) => memory_skill::run(args, &state_dir, &config),
63 cli::Commands::Watch(args) => reviewer::run_watch_command(args, &state_dir, &config),
64 cli::Commands::Status(_) => unreachable!("status returns before config load"),
65 cli::Commands::Skills(args) => skills::run(args),
66 cli::Commands::HookDispatch(args) => {
67 hooks::dispatch(args, &state_dir, config_path.as_deref(), &config)
68 }
69 }
70}