Launcher: CLI subcommands and an MCP server over stdio, both on the shared core
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
//! `horizon-launcher <subcommand>`: the same steps as the window, from a terminal. Every
|
||||
//! command goes through `backend`; this file only parses arguments and prints.
|
||||
use clap::{Parser, Subcommand};
|
||||
use horizon_launcher::backend::{self, Release};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "horizon-launcher", version, about = "Installs, prepares and starts Horizon. Without a subcommand, opens the window.")]
|
||||
struct Cli {
|
||||
/// Print JSON instead of text, where a command has something to report
|
||||
#[arg(long, global = true)]
|
||||
json: bool,
|
||||
#[command(subcommand)]
|
||||
command: Option<Cmd>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Cmd {
|
||||
/// List Horizon releases, or the installed versions
|
||||
Versions {
|
||||
/// Only the versions installed here (no network)
|
||||
#[arg(long)]
|
||||
installed: bool,
|
||||
},
|
||||
/// Download and install a release (a tag such as v2026.10.13, or "latest")
|
||||
Install { tag: String },
|
||||
/// Remove an installed version
|
||||
Remove { tag: String },
|
||||
/// Prepare a version's resources from your own Minecraft install
|
||||
Prepare {
|
||||
/// The official launcher's game folder (default: the one the launcher uses)
|
||||
#[arg(long)]
|
||||
minecraft: Option<PathBuf>,
|
||||
/// The installed version (default: the selected one)
|
||||
#[arg(long)]
|
||||
version: Option<String>,
|
||||
},
|
||||
/// Start the client
|
||||
Play {
|
||||
#[arg(long)]
|
||||
version: Option<String>,
|
||||
},
|
||||
/// Run a dedicated server here; type commands into its console
|
||||
Server {
|
||||
/// The server's folder (default: the one the launcher uses)
|
||||
#[arg(long)]
|
||||
dir: Option<PathBuf>,
|
||||
#[arg(long)]
|
||||
version: Option<String>,
|
||||
/// Agree to the Minecraft EULA (https://aka.ms/MinecraftEULA)
|
||||
#[arg(long)]
|
||||
accept_eula: bool,
|
||||
},
|
||||
/// Show a release's patch notes (default: the newest)
|
||||
Notes { tag: Option<String> },
|
||||
/// Show the roadmap
|
||||
Next,
|
||||
/// Look for a newer Horizon release and launcher, and install them
|
||||
Update {
|
||||
/// Only report what is available
|
||||
#[arg(long)]
|
||||
check: bool,
|
||||
},
|
||||
/// Serve the Model Context Protocol on stdin and stdout, for AI agents
|
||||
Mcp,
|
||||
}
|
||||
|
||||
/// Runs a subcommand and returns the exit code, or None when the window should open.
|
||||
pub fn run() -> Option<i32> {
|
||||
let cli = Cli::parse();
|
||||
let command = cli.command?;
|
||||
let result = match command {
|
||||
Cmd::Mcp => crate::mcp::serve(),
|
||||
command => execute(command, cli.json),
|
||||
};
|
||||
Some(match result {
|
||||
Ok(()) => 0,
|
||||
Err(e) => {
|
||||
eprintln!("error: {e}");
|
||||
1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn progress(label: String) -> impl Fn(f32) {
|
||||
move |p| {
|
||||
eprint!("\r{label} {:>3.0}%", p * 100.);
|
||||
let _ = std::io::stderr().flush();
|
||||
if p >= 1.0 {
|
||||
eprintln!();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn json(value: serde_json::Value) {
|
||||
println!("{}", serde_json::to_string_pretty(&value).unwrap());
|
||||
}
|
||||
|
||||
/// Runs a game process in this terminal and reports how it ended.
|
||||
fn attached(mut cmd: Command) -> Result<(), String> {
|
||||
let status = cmd.status().map_err(|e| format!("Could not start {:?}: {e}", cmd.get_program()))?;
|
||||
if status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Exited with {status}"))
|
||||
}
|
||||
}
|
||||
|
||||
fn release_json(r: &Release) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"tag": r.tag, "name": r.name, "date": r.date,
|
||||
"available_here": r.asset.is_some(), "installed": backend::installed().contains(&r.tag),
|
||||
})
|
||||
}
|
||||
|
||||
fn execute(command: Cmd, as_json: bool) -> Result<(), String> {
|
||||
match command {
|
||||
Cmd::Versions { installed: true } => {
|
||||
let installed = backend::installed();
|
||||
let selected = backend::resolve_version(None).ok();
|
||||
if as_json {
|
||||
json(serde_json::json!({ "installed": installed, "selected": selected }));
|
||||
} else if installed.is_empty() {
|
||||
println!("No version is installed.");
|
||||
}
|
||||
for tag in installed.iter().filter(|_| !as_json) {
|
||||
let mark = if selected.as_ref() == Some(tag) { "*" } else { " " };
|
||||
let state = if backend::is_prepared(tag) { "prepared" } else { "not prepared" };
|
||||
println!("{mark} {tag} {state}");
|
||||
}
|
||||
}
|
||||
Cmd::Versions { installed: false } => {
|
||||
let releases = backend::releases()?;
|
||||
if as_json {
|
||||
json(releases.iter().map(release_json).collect());
|
||||
return Ok(());
|
||||
}
|
||||
let installed = backend::installed();
|
||||
for r in &releases {
|
||||
let state = match (installed.contains(&r.tag), r.asset.is_some()) {
|
||||
(true, _) => "installed",
|
||||
(false, true) => "",
|
||||
(false, false) => "no build for this platform",
|
||||
};
|
||||
println!("{:<14} {} {}", r.tag, r.date, state);
|
||||
}
|
||||
}
|
||||
Cmd::Install { tag } => {
|
||||
let label = format!("Installing {tag}");
|
||||
let tag = backend::install_tag(&tag, false, progress(label))?;
|
||||
if as_json {
|
||||
json(serde_json::json!({ "installed": tag }));
|
||||
} else {
|
||||
println!("Installed {tag}. Prepare it next: horizon-launcher prepare --version {tag}");
|
||||
}
|
||||
}
|
||||
Cmd::Remove { tag } => {
|
||||
backend::remove_version(&tag)?;
|
||||
println!("Removed {tag}.");
|
||||
}
|
||||
Cmd::Prepare { minecraft, version } => {
|
||||
let tag = backend::resolve_version(version.as_deref())?;
|
||||
let minecraft = backend::minecraft_dir(minecraft)?;
|
||||
let cmd = backend::prepare_command(&tag, &minecraft, &|line| eprintln!("{line}"))?;
|
||||
attached(cmd)?;
|
||||
println!("{tag} is prepared.");
|
||||
}
|
||||
Cmd::Play { version } => attached(backend::client(&backend::resolve_version(version.as_deref())?)?)?,
|
||||
Cmd::Server { dir, version, accept_eula } => {
|
||||
let tag = backend::resolve_version(version.as_deref())?;
|
||||
let dir = backend::server_dir(dir);
|
||||
// The server inherits this terminal, so typed lines reach its console.
|
||||
attached(backend::server(&tag, &dir, accept_eula)?)?
|
||||
}
|
||||
Cmd::Notes { tag } => {
|
||||
let r = backend::release_notes(tag.as_deref())?;
|
||||
if as_json {
|
||||
json(serde_json::json!({ "tag": r.tag, "name": r.name, "date": r.date, "notes": r.body }));
|
||||
} else {
|
||||
let title = if r.name.is_empty() { &r.tag } else { &r.name };
|
||||
println!("{title}\n{} {}\n\n{}", r.tag, r.date, backend::markdown_to_text(&r.body));
|
||||
}
|
||||
}
|
||||
Cmd::Next => {
|
||||
let roadmap = backend::roadmap()?;
|
||||
if as_json {
|
||||
json(serde_json::json!({ "roadmap": roadmap }));
|
||||
} else {
|
||||
print!("{}", backend::markdown_to_text(&roadmap));
|
||||
}
|
||||
}
|
||||
Cmd::Update { check } => update(check, as_json)?,
|
||||
Cmd::Mcp => unreachable!(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update(check: bool, as_json: bool) -> Result<(), String> {
|
||||
let updates = backend::check_updates()?;
|
||||
if as_json && check {
|
||||
json(serde_json::json!({
|
||||
"horizon": updates.horizon.as_ref().map(|r| &r.tag),
|
||||
"launcher": updates.launcher.as_ref().and_then(|r| backend::launcher_version(&r.tag)),
|
||||
"launcher_current": backend::VERSION,
|
||||
}));
|
||||
return Ok(());
|
||||
}
|
||||
match &updates.horizon {
|
||||
None => println!("Horizon: up to date."),
|
||||
Some(r) if check => println!("Horizon: {} is available.", r.tag),
|
||||
Some(r) => {
|
||||
let tag = backend::install_tag(&r.tag, true, progress(format!("Installing {}", r.tag)))?;
|
||||
println!("Horizon: installed {tag}.");
|
||||
}
|
||||
}
|
||||
match &updates.launcher {
|
||||
None => println!("Launcher: {} is up to date.", backend::VERSION),
|
||||
Some(r) => {
|
||||
let version = backend::launcher_version(&r.tag).unwrap_or_default();
|
||||
if check {
|
||||
println!("Launcher: {version} is available (this is {}).", backend::VERSION);
|
||||
} else if !backend::self_update_allowed() {
|
||||
println!("Launcher: {version} is available; this build does not replace itself.");
|
||||
} else {
|
||||
backend::self_update(r, progress(format!("Updating the launcher to {version}")))?;
|
||||
println!("Launcher: updated to {version}; the next start uses it.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user