mod export;
mod home;
mod mcp;
mod service;
mod store;
mod sync;
mod update;
mod web;
use std::path::PathBuf;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
use crate::home::Home;
#[derive(Parser)]
#[command(name = "kanzo", version)]
struct Cli {
#[arg(long, global = true, value_name = "DIR")]
home: Option<PathBuf>,
#[arg(long)]
port: Option<u16>,
#[arg(long)]
no_open: bool,
#[command(subcommand)]
command: Option<Command>,
}
#[derive(Subcommand)]
enum Command {
Mcp,
Init {
folder: Option<PathBuf>,
},
Sync {
#[arg(long, value_name = "URL")]
repo: Option<String>,
#[arg(long, conflicts_with = "keep_remote")]
keep_local: bool,
#[arg(long)]
keep_remote: bool,
},
Export {
#[arg(long, short, value_enum, default_value = "markdown")]
format: export::Format,
#[arg(long, short)]
project: Option<String>,
#[arg(long, short, value_name = "FILE")]
output: Option<PathBuf>,
},
Update,
Service {
#[command(subcommand)]
action: ServiceAction,
},
}
#[derive(Subcommand)]
enum ServiceAction {
Install,
Uninstall,
}
fn main() -> ExitCode {
let cli = Cli::parse();
let outcome = tokio::runtime::Builder::new_current_thread()
.enable_all()
.max_blocking_threads(8)
.build()
.map_err(anyhow::Error::from)
.and_then(|rt| rt.block_on(run(cli)));
match outcome {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("kanzo: {e:#}");
ExitCode::FAILURE
}
}
}
async fn run(cli: Cli) -> anyhow::Result<()> {
if let Some(Command::Update) = cli.command {
return update_kanzo();
}
let dir = match &cli.command {
Some(Command::Init { folder: Some(folder) }) => folder.clone(),
_ => Home::resolve(cli.home)?,
};
let home = Home::open(dir)?;
match cli.command {
None => web::serve(home, cli.port, !cli.no_open).await,
Some(Command::Mcp) => mcp::serve(home).await,
Some(Command::Init { .. }) => {
println!("Kanzo data folder ready at {}", home.dir.display());
Ok(())
}
Some(Command::Sync { repo, keep_local, keep_remote }) => {
if let Some(url) = repo {
println!("The board syncs with {}.", sync::set_remote(&home, &url, true)?);
}
let keep = match (keep_local, keep_remote) {
(true, _) => Some(sync::Keep::Local),
(_, true) => Some(sync::Keep::Remote),
_ => None,
};
println!("{}", sync::sync(&home, keep, true)?);
Ok(())
}
Some(Command::Export { format, project, output }) => {
export::export(&home, format, project.as_deref(), output.as_deref())
}
Some(Command::Service { action: ServiceAction::Install }) => service::install(&home),
Some(Command::Service { action: ServiceAction::Uninstall }) => service::uninstall(),
Some(Command::Update) => unreachable!("handled above"),
}
}
fn update_kanzo() -> anyhow::Result<()> {
let Some(latest) = update::latest(true) else {
anyhow::bail!("can't reach GitHub to check for updates");
};
if !update::newer(&latest) {
println!("Kanzo is up to date ({}).", update::CURRENT);
return Ok(());
}
println!("Installing Kanzo {latest} (you have {})…", update::CURRENT);
update::install().map_err(anyhow::Error::msg)?;
if service::restart()? {
println!("Updated to {latest}, and restarted the board.");
} else {
println!("Updated to {latest}.");
}
Ok(())
}