use anyhow::Result;
use clap::{Parser, Subcommand};
use org_core::OrgMode;
mod commands;
mod config;
use commands::{
AgendaCommand, CaptureCommand, ConfigCommand, ElementByIdCommand, HeadingCommand, ListCommand,
OutlineCommand, ReadCommand, SearchCommand, UpdateTodoCommand,
};
use config::CliAppConfig;
#[derive(Parser)]
#[command(name = "agd")]
#[command(about = "Local-first command-line tools for org-mode files")]
#[command(version = env!("CARGO_PKG_VERSION"))]
struct Cli {
#[arg(short, long)]
config: Option<String>,
#[arg(short, long)]
root_directory: Option<String>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Agenda(AgendaCommand),
Capture(Box<CaptureCommand>),
Config(ConfigCommand),
List(ListCommand),
Read(ReadCommand),
Outline(OutlineCommand),
Heading(HeadingCommand),
ElementById(ElementByIdCommand),
Search(SearchCommand),
UpdateTodo(Box<UpdateTodoCommand>),
}
fn main() {
if let Err(e) = run() {
eprintln!("Error: {e:#}");
std::process::exit(1);
}
}
fn run() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Config(cmd) => cmd.execute(cli.config),
_ => {
let config = CliAppConfig::load(
cli.config,
cli.root_directory,
None, )?;
let org_mode = OrgMode::new(config.org)?;
match cli.command {
Commands::Agenda(cmd) => cmd.execute(org_mode, config.cli),
Commands::Capture(cmd) => cmd.execute(org_mode, config.cli),
Commands::Config(_) => unreachable!(),
Commands::List(cmd) => cmd.execute(org_mode, config.cli),
Commands::Read(cmd) => cmd.execute(org_mode, config.cli),
Commands::Outline(cmd) => cmd.execute(org_mode, config.cli),
Commands::Heading(cmd) => cmd.execute(org_mode, config.cli),
Commands::ElementById(cmd) => cmd.execute(org_mode, config.cli),
Commands::Search(cmd) => cmd.execute(org_mode, config.cli),
Commands::UpdateTodo(cmd) => cmd.execute(org_mode, config.cli),
}
}
}
}