Skip to content

armgit / kanzo

Public Imported from GitHub
0

Self-hosted, sharable kanban for humans and agents. One 4.7 MB binary, 5 MB of RAM, 10 to 60 tokens per agent reply, starts in 3 ms, zero setup.

6.7 KB
Raw Blame
mod export;
mod home;
mod mcp;
mod service;
mod share;
mod store;
mod sync;
mod update;
mod web;

use std::path::PathBuf;
use std::process::ExitCode;

use clap::{Parser, Subcommand, ValueEnum};

use crate::home::Home;

/// Self-hosted kanban for humans and agents.
///
/// Run `kanzo` to open the board. Agents connect with `kanzo mcp`.
#[derive(Parser)]
#[command(name = "kanzo", version)]
struct Cli {
    /// Data folder [default: $KANZO_HOME, then ~/kanzo]
    #[arg(long, global = true, value_name = "DIR")]
    home: Option<PathBuf>,

    /// Port for the board [default: from config.toml]
    #[arg(long)]
    port: Option<u16>,

    /// Start the board without opening the browser
    #[arg(long)]
    no_open: bool,

    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Subcommand)]
enum Command {
    /// Run the MCP server on stdio (agents launch this, not you)
    Mcp,
    /// Create the data folder; the first run does this too
    Init {
        /// Folder to create [default: the data folder]
        folder: Option<PathBuf>,
    },
    /// Commit the data folder and pull or push it with its git remote
    Sync {
        /// Sync with this git repo from now on, like https://github.com/you/kanzo-data or you/kanzo-data
        #[arg(long, value_name = "URL")]
        repo: Option<String>,
        /// When both sides changed, keep this machine's board and overwrite the remote
        #[arg(long, conflicts_with = "keep_remote")]
        keep_local: bool,
        /// When both sides changed, take the remote board and drop this machine's changes
        #[arg(long)]
        keep_remote: bool,
    },
    /// Write the whole board as Markdown or JSON
    Export {
        /// Output format
        #[arg(long, short, value_enum, default_value = "markdown")]
        format: export::Format,
        /// Only this project
        #[arg(long, short)]
        project: Option<String>,
        /// Write to this file instead of the terminal
        #[arg(long, short, value_name = "FILE")]
        output: Option<PathBuf>,
    },
    /// Share the board with other computers on your network, or stop; without on or off, show whether it is
    Share {
        #[arg(value_enum)]
        state: Option<OnOff>,
    },
    /// Use the board another computer shares, from this computer's board and agents
    Join {
        /// The address `kanzo share on` printed there, like 192.168.0.12
        host: String,
    },
    /// Stop using the shared board, and go back to this computer's own
    Leave,
    /// Install the latest release over this Kanzo, and restart the board's service
    Update,
    /// Start the board automatically at login
    Service {
        #[command(subcommand)]
        action: ServiceAction,
    },
}

#[derive(Clone, Copy, ValueEnum)]
enum OnOff {
    On,
    Off,
}

#[derive(Subcommand)]
enum ServiceAction {
    /// Install and start a service that runs the board at login (LaunchAgent on macOS, systemd on Linux, Task Scheduler on Windows)
    Install,
    /// Stop and remove that service
    Uninstall,
}

fn main() -> ExitCode {
    let cli = Cli::parse();
    if cfg!(windows)
        && let Ok(exe) = std::env::current_exe()
    {
        update::remove_old_exes(&exe);
    }
    // The board serves one person and each agent starts its own `kanzo mcp`, so one async
    // thread is enough, and blocking threads exit when idle: this keeps kanzo light on small boards.
    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<()> {
    // Before opening the data folder: it may be the reason to update.
    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 mut home = Home::open(dir)?;
    if let Some(port) = cli.port {
        home.config.port = port;
    }
    match cli.command {
        None => web::serve(home, !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::Share { state }) => {
            let change = state.map(|s| match s {
                OnOff::On => share::Change::On,
                OnOff::Off => share::Change::Off,
            });
            network(home, change, !cli.no_open).await
        }
        Some(Command::Join { host }) => network(home, Some(share::Change::Join(host)), !cli.no_open).await,
        Some(Command::Leave) => network(home, Some(share::Change::Leave), !cli.no_open).await,
        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(())
}

async fn network(home: Home, change: Option<share::Change>, open: bool) -> anyhow::Result<()> {
    let sharing = matches!(change, Some(share::Change::On));
    let (status, running) = share::request(&home, change).await?;
    if sharing && !running {
        return web::serve(home, open).await;
    }
    println!("{}", status.message());
    Ok(())
}