Merge pull request #1017 from ludovic111/feat/launcher-servers-backend
🤖 Agent: merged after verify passed (launcher-only; no parity harness depends on it). The launcher's server backend and `horizon-launcher servers …` CLI.
This commit is contained in:
@@ -21,6 +21,58 @@ struct Cli {
|
||||
command: Option<Cmd>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum ServersCmd {
|
||||
/// The servers, their folder and whether they run
|
||||
List,
|
||||
/// Create a server; with no options it is a new world, survival, normal, 10 players, friends only
|
||||
Create {
|
||||
#[arg(long, default_value = "My server")]
|
||||
name: String,
|
||||
/// The new world's seed (default: random)
|
||||
#[arg(long, conflicts_with = "copy_world")]
|
||||
seed: Option<String>,
|
||||
/// Start from a copy of this world folder (one of your saves)
|
||||
#[arg(long)]
|
||||
copy_world: Option<PathBuf>,
|
||||
#[arg(long, default_value = "survival", value_parser = ["survival", "creative", "adventure", "spectator"])]
|
||||
mode: String,
|
||||
#[arg(long, default_value = "normal", value_parser = ["peaceful", "easy", "normal", "hard"])]
|
||||
difficulty: String,
|
||||
#[arg(long, default_value_t = 10)]
|
||||
max_players: u32,
|
||||
/// Let anyone in (no whitelist)
|
||||
#[arg(long)]
|
||||
open: bool,
|
||||
/// Check players' Minecraft accounts (they must sign in with Microsoft)
|
||||
#[arg(long)]
|
||||
check_accounts: bool,
|
||||
/// Who may join (repeatable); the first becomes an operator
|
||||
#[arg(long = "player")]
|
||||
players: Vec<String>,
|
||||
/// The installed version whose programs and game files servers use, if none are set up yet
|
||||
#[arg(long)]
|
||||
version: Option<String>,
|
||||
/// Agree to the Minecraft EULA (https://aka.ms/MinecraftEULA)
|
||||
#[arg(long)]
|
||||
accept_eula: bool,
|
||||
},
|
||||
/// Copy an installed, prepared version's programs and game files for the servers (again)
|
||||
SetUp {
|
||||
#[arg(long)]
|
||||
version: Option<String>,
|
||||
},
|
||||
/// Run a horizon-server command on a server, for example: servers run "My server" -- status
|
||||
Run {
|
||||
/// The server's name or folder
|
||||
server: String,
|
||||
#[arg(last = true, required = true)]
|
||||
args: Vec<String>,
|
||||
},
|
||||
/// Take a server off the list (its folder stays)
|
||||
Forget { server: String },
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum AccountCmd {
|
||||
/// The signed-in accounts and which one plays
|
||||
@@ -205,6 +257,12 @@ enum Cmd {
|
||||
#[command(subcommand)]
|
||||
action: Option<ModpackCmd>,
|
||||
},
|
||||
/// Servers hosted with horizon-server (the Servers page): list them, create one, or run any
|
||||
/// horizon-server command on one
|
||||
Servers {
|
||||
#[command(subcommand)]
|
||||
action: Option<ServersCmd>,
|
||||
},
|
||||
/// Serve the Model Context Protocol on stdin and stdout, for AI agents
|
||||
Mcp,
|
||||
/// Measure the launcher: starts (the first is the cold one), page switching, idle CPU and
|
||||
@@ -465,6 +523,7 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> {
|
||||
}
|
||||
}
|
||||
Cmd::Modpack { action } => modpack(action.unwrap_or(ModpackCmd::List), as_json)?,
|
||||
Cmd::Servers { action } => servers(action.unwrap_or(ServersCmd::List), as_json)?,
|
||||
Cmd::Mcp => unreachable!(),
|
||||
}
|
||||
Ok(())
|
||||
@@ -657,6 +716,111 @@ fn data(action: DataCmd, as_json: bool) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn servers(action: ServersCmd, as_json: bool) -> Result<(), String> {
|
||||
use horizon_launcher::servers::{self, NewServer, World};
|
||||
let find = |which: &str| {
|
||||
servers::list()
|
||||
.into_iter()
|
||||
.find(|e| e.name == which || e.dir == std::path::Path::new(which))
|
||||
.ok_or_else(|| {
|
||||
format!("No server called {which}: `horizon-launcher servers` lists them")
|
||||
})
|
||||
};
|
||||
match action {
|
||||
ServersCmd::List => {
|
||||
let list = servers::list();
|
||||
if as_json {
|
||||
json(serde_json::Value::Array(
|
||||
list.iter()
|
||||
.map(|e| {
|
||||
serde_json::json!({
|
||||
"name": e.name, "dir": e.dir,
|
||||
"phase": servers::status(&e.dir).phase,
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
));
|
||||
} else if list.is_empty() {
|
||||
println!("No server yet: horizon-launcher servers create --accept-eula");
|
||||
}
|
||||
for e in list.iter().filter(|_| !as_json) {
|
||||
println!(
|
||||
"{} ({}): {}",
|
||||
e.name,
|
||||
e.dir.display(),
|
||||
servers::status(&e.dir).phase
|
||||
);
|
||||
}
|
||||
}
|
||||
ServersCmd::Create {
|
||||
name,
|
||||
seed,
|
||||
copy_world,
|
||||
mode,
|
||||
difficulty,
|
||||
max_players,
|
||||
open,
|
||||
check_accounts,
|
||||
players,
|
||||
version,
|
||||
accept_eula,
|
||||
} => {
|
||||
let spec = NewServer {
|
||||
name,
|
||||
world: match copy_world {
|
||||
Some(dir) => World::Copy(dir),
|
||||
None => World::New {
|
||||
seed: seed.unwrap_or_default(),
|
||||
},
|
||||
},
|
||||
game_mode: mode,
|
||||
difficulty,
|
||||
max_players,
|
||||
friends_only: !open,
|
||||
check_accounts,
|
||||
players,
|
||||
eula: accept_eula,
|
||||
};
|
||||
let tag = backend::resolve_version(version.as_deref()).ok();
|
||||
let entry = servers::create(&spec, tag.as_deref(), &|l| eprintln!("{l}"))
|
||||
.map_err(|p| p.message)?;
|
||||
if as_json {
|
||||
json(serde_json::json!({ "name": entry.name, "dir": entry.dir }));
|
||||
} else {
|
||||
println!("Created {} in {}", entry.name, entry.dir.display());
|
||||
}
|
||||
}
|
||||
ServersCmd::SetUp { version } => {
|
||||
let tag = backend::resolve_version(version.as_deref())?;
|
||||
servers::set_up(&tag)?;
|
||||
println!(
|
||||
"Servers run Horizon {}",
|
||||
servers::runtime_release().unwrap_or(tag)
|
||||
);
|
||||
}
|
||||
ServersCmd::Run { server, args } => {
|
||||
let entry = find(&server)?;
|
||||
let args: Vec<&str> = args.iter().map(String::as_str).collect();
|
||||
let status = servers::command(&entry.dir, &args)?
|
||||
.status()
|
||||
.map_err(|e| e.to_string())?;
|
||||
if !status.success() {
|
||||
return Err(format!("horizon-server exited with {status}"));
|
||||
}
|
||||
}
|
||||
ServersCmd::Forget { server } => {
|
||||
let entry = find(&server)?;
|
||||
servers::forget(&entry.dir)?;
|
||||
println!(
|
||||
"{} is off the list; its folder stays in {}",
|
||||
entry.name,
|
||||
entry.dir.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn modpack(action: ModpackCmd, as_json: bool) -> Result<(), String> {
|
||||
use horizon_launcher::modpacks::{self, Imported};
|
||||
use horizon_port_pack::report::Status;
|
||||
|
||||
Reference in New Issue
Block a user