use std::process::ExitCode;
use anyhow::Result;
use clap::{Parser, Subcommand};
use org_core::AgdError;
mod commands;
mod importer;
mod mcp;
mod tui;
use commands::{AgendaCommand, CaptureCommand, SetupCommand};
use mcp::McpCommand;
#[derive(Parser)]
#[command(name = "agd", version)]
struct Cli {
#[arg(short, long, global = true)]
config: Option<String>,
#[arg(short, long, global = true)]
root_directory: Option<String>,
#[arg(long)]
pane: bool,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Agenda(AgendaCommand),
Capture(CaptureCommand),
Mcp(McpCommand),
Setup(SetupCommand),
}
fn main() -> ExitCode {
match run() {
Ok(true) => ExitCode::FAILURE,
Ok(false) => ExitCode::SUCCESS,
Err(error) => {
let json_requested = std::env::args_os().any(|argument| argument == "--json");
if json_requested && let Some(error) = error.downcast_ref::<AgdError>() {
eprintln!(
"{}",
serde_json::to_string(error.response())
.unwrap_or_else(|_| format!(r#"{{"message":"{error}"}}"#))
);
} else {
eprintln!("Error: {error:#}");
}
ExitCode::FAILURE
}
}
}
fn run() -> Result<bool> {
let Cli {
config,
root_directory,
pane,
command,
} = Cli::parse();
match command {
None => tui::run(config, root_directory, pane).map(|()| false),
Some(Commands::Agenda(command)) => command.execute(config, root_directory).map(|()| false),
Some(Commands::Capture(command)) => command.execute(config, root_directory).map(|()| false),
Some(Commands::Mcp(command)) => command.execute(config, root_directory).map(|()| false),
Some(Commands::Setup(command)) => command.execute(config, root_directory),
}
}