1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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);
}
// One thread runs all the async work: the board serves one person, and each
// agent starts its own `kanzo mcp`. Database calls run on a few blocking
// threads that 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)?;
// `--port` reaches the board on that port, for `kanzo share` and the others too.
if let Some(port) = cli.port {
home.config.port = port;
}
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::Share { state }) => {
let change = state.map(|s| match s {
OnOff::On => share::Change::On,
OnOff::Off => share::Change::Off,
});
let (status, running) = share::request(&home, change).await?;
// Sharing with no board running starts one, already shared.
if matches!(state, Some(OnOff::On)) && !running {
return web::serve(home, cli.port, !cli.no_open).await;
}
println!("{}", status.message());
Ok(())
}
Some(Command::Join { host }) => {
println!("{}", share::request(&home, Some(share::Change::Join(host))).await?.0.message());
Ok(())
}
Some(Command::Leave) => {
println!("{}", share::request(&home, Some(share::Change::Leave)).await?.0.message());
Ok(())
}
Some(Command::Service { action: ServiceAction::Install }) => service::install(&home),
Some(Command::Service { action: ServiceAction::Uninstall }) => service::uninstall(),
Some(Command::Update) => unreachable!("handled above"),
}
}
/// `kanzo update`: installs the latest release over this program, then restarts the
/// board's service, if there is one, so it runs the new version too.
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(())
}