From e52763c35e046aa43d3a8786a8e3d1dec7db3045 Mon Sep 17 00:00:00 2001 From: tomoron Date: Fri, 5 Jun 2026 00:38:12 +0200 Subject: [PATCH] options to env handling --- src/cli/add.rs | 23 +++++++++++++--- src/cli/env_settings.rs | 60 +++++++++++++++++++++++++++++++++++++++++ src/cli/mod.rs | 4 +++ src/cli/utils.rs | 3 +++ src/main.rs | 8 +++--- 5 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/cli/env_settings.rs create mode 100644 src/cli/utils.rs diff --git a/src/cli/add.rs b/src/cli/add.rs index d50994e..dbed0c7 100644 --- a/src/cli/add.rs +++ b/src/cli/add.rs @@ -1,12 +1,27 @@ use clap::{ArgMatches, Command, arg}; +use crate::cli::env_settings; +use crate::utils::validate_name; pub fn create_subcommand() -> Command { + let mut arguments = vec![ + arg!( "Name of the server"), + arg!([SERVER_ZIP] "initial server zip"), + ]; + + arguments.extend(env_settings::get_args()); + Command::new("add") .about("add a server") - .arg(arg!( "Name of the server")) - .arg(arg!(-v --version "minecraft version of the server")) + .args(arguments.as_slice()) } -pub fn subcommand(arg: &ArgMatches) { - println!("got add, sub : {:?}", arg); + +pub fn subcommand(arg: &ArgMatches) -> Result<(), String>{ + if !validate_name(arg.get_one::("NAME").unwrap()) { + return Err("Invalid name provided".to_string()); + } + + + println!("{}", env_settings::args_to_env(arg)); + Ok(()) } diff --git a/src/cli/env_settings.rs b/src/cli/env_settings.rs new file mode 100644 index 0000000..6594299 --- /dev/null +++ b/src/cli/env_settings.rs @@ -0,0 +1,60 @@ +use clap::{ Arg, ArgMatches, ArgAction}; + +pub fn get_args() -> [Arg; 22] { + [ + Arg::new("VERSION").short('v').long("version").help("Minecraft version of the server").default_value("LATEST"), + Arg::new("MEMORY").short('m').long("memory").help("How much ram does this server have").default_value("4G"), + Arg::new("MAX_PLAYERS").short('p').long("max-players").help("Max players").default_value("20"), + Arg::new("SPAWN_PROTECTION").short('s').long("spawn-protection").help("spawn protection size").default_value("0"), + Arg::new("ONLINE_MODE").short('o').long("online-mode").help("false to allow cracked accounts to join").default_value("true"), + Arg::new("TYPE").short('t').long("type").help("Server type (VANILLA, FORGE, NEOFORGE, PAPER)").default_value("VANILLA"), + Arg::new("FORGE_VERSION").long("forge-version").help("forge version if set type is FORGE or NEOFORGE").default_value("latest"), //TODO diff handle + Arg::new("MOTD").short('M').long("motd").help("MOTD of the server (only shown when the server is running)"), + Arg::new("DIFFICULTY").short('d').long("difficulty").help("Difficulty of the server").default_value("easy"), + Arg::new("WHITELIST").short('w').long("whitelist").help("comma separated list of the playsrs to whitelist (disabled if not provied)"), //TODO diff handle + Arg::new("OPS").short('O').long("ops").help("comma separated list of the players to give operator permissions to"), + Arg::new("MODE").long("gamemode").default_value("survival"), + Arg::new("CUSTOM_SERVER_PROPERTIES").long("custom-property").help("add a custom property").action(ArgAction::Append), //TODO diff handle + Arg::new("ALLOW_FLIGHT").short('f').long("alllow-flight").help("Allow flight in suvival mode").default_value("false"), + Arg::new("ALLOW_NETHER").short('n').long("allow-nether").help("Enable the nether dimension").default_value("true"), + Arg::new("HARDCORE").short('H').long("hardcore").help("Enable hardcore mode").default_value("false"), + Arg::new("MAX_TICK_TIME").short('T').long("max-tick-time").help("how long should it wait for a tick before crashing").default_value("-1").allow_negative_numbers(true), + Arg::new("PAUSE_WHEN_EMPTY_SECONDS").long("pause-when-empty-seconds").help("pause when empty for more than selected number of seconds").default_value("-1").allow_negative_numbers(true), + Arg::new("PVP").short('P').long("pvp").help("enable pvp").default_value("true"), + Arg::new("SPAWN_ANIMALS").long("spawn-animals").help("spawn animals").default_value("true"), + Arg::new("SPAWN_MONSTERS").long("spawn-monsters").help("spawn monsters").default_value("true"), + Arg::new("SPAWN_NPCS").long("spawn-npcs").help("spawn npcs").default_value("true"), + ] +} + +pub fn args_to_env(arguments: &ArgMatches) -> String { + let default_handle = vec!["VERSION", "MEMORY", "MAX_PLAYERS", "SPAWN_PROTECTION", "ONLINE_MODE", "TYPE", "MOTD", "DIFFICULTY", "OPS", "MODE", "ALLOW_FLIGHT", + "ALLOW_NETHER", "HARDCORE", "MAX_TICK_TIME", "PAUSE_WHEN_EMPTY_SECONDS", "PVP", "SPAWN_ANIMALS", "SPAWN_MONSTERS", "SPAWN_NPCS"]; + let mut res = vec![]; + + for setting in default_handle { + if let Some(val) = arguments.get_one::(setting) { + res.push((setting.to_string(), val.to_string())); + } + } + + if let Some(value) = arguments.get_one::("FORGE_VERSION") { + res.push(("FORGE_VERSION".to_string(), value.to_string())); + res.push(("NEOFORGE_VERSION".to_string(), value.to_string())); + } + + if let Some(value) = arguments.get_one::("WHITELIST") { + res.push(("ENABLE_WHITELIST".to_string(), "true".to_string())); + res.push(("WHITELIST".to_string(),value.to_string())); + } + + if let Some(value) = arguments.get_many::("CUSTOM_SERVER_PROPERTIES") { + let properties = value.collect::>().iter().map(|x| x.to_string()).collect::>().join("\n"); + res.push(("CUSTOM_SERVER_PROPERTIES".to_string(), properties)); + } + +// println!("\n\n\narguments : {:?}\nignored:{:?}", arguments, ignored); + res.iter() + .map(|x| format!("{}=\"{}\"", x.0, x.1)) + .collect::>().join("\n") +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 3d50c16..e7aadbe 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -2,3 +2,7 @@ pub mod add; pub mod edit; pub mod remove; pub mod ls; + +pub mod env_settings; + +pub mod utils; diff --git a/src/cli/utils.rs b/src/cli/utils.rs new file mode 100644 index 0000000..e8bc185 --- /dev/null +++ b/src/cli/utils.rs @@ -0,0 +1,3 @@ +pub fn validate_name(name: &str) -> bool{ + name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '-') +} diff --git a/src/main.rs b/src/main.rs index 9e69c35..0f3e223 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/05/29 21:22:17 by tomoron #+# #+# */ -/* Updated: 2026/06/04 12:37:14 by tomoron ### ########.fr */ +/* Updated: 2026/06/05 00:11:54 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,13 +74,13 @@ fn cli() -> Command { } #[tokio::main(flavor = "current_thread")] -async fn main() -> io::Result<()> { +async fn main() -> Result<(), String> { let arg = cli().get_matches(); let pool = get_connection_pool(); match arg.subcommand() { - Some(("serverMode", _)) => { mc_socket_listen(pool).await?; }, - Some(("add", sub_matches)) => { add::subcommand(sub_matches); }, + Some(("serverMode", _)) => { mc_socket_listen(pool).await.map_err(|_| "socket error".to_string())?; }, + Some(("add", sub_matches)) => { add::subcommand(sub_matches)?; }, _ => {panic!("subcommand not implemented")} }