options to env handling

This commit is contained in:
2026-06-05 00:38:12 +02:00
parent 37ce941e02
commit e52763c35e
5 changed files with 90 additions and 8 deletions

View File

@ -1,12 +1,27 @@
use clap::{ArgMatches, Command, arg}; use clap::{ArgMatches, Command, arg};
use crate::cli::env_settings;
use crate::utils::validate_name;
pub fn create_subcommand() -> Command { pub fn create_subcommand() -> Command {
let mut arguments = vec![
arg!(<NAME> "Name of the server"),
arg!([SERVER_ZIP] "initial server zip"),
];
arguments.extend(env_settings::get_args());
Command::new("add") Command::new("add")
.about("add a server") .about("add a server")
.arg(arg!(<NAME> "Name of the server")) .args(arguments.as_slice())
.arg(arg!(-v --version <VERSION> "minecraft version of the server"))
} }
pub fn subcommand(arg: &ArgMatches) {
println!("got add, sub : {:?}", arg); pub fn subcommand(arg: &ArgMatches) -> Result<(), String>{
if !validate_name(arg.get_one::<String>("NAME").unwrap()) {
return Err("Invalid name provided".to_string());
}
println!("{}", env_settings::args_to_env(arg));
Ok(())
} }

60
src/cli/env_settings.rs Normal file
View File

@ -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::<String>(setting) {
res.push((setting.to_string(), val.to_string()));
}
}
if let Some(value) = arguments.get_one::<String>("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::<String>("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::<String>("CUSTOM_SERVER_PROPERTIES") {
let properties = value.collect::<Vec<&String>>().iter().map(|x| x.to_string()).collect::<Vec<String>>().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::<Vec<String>>().join("\n")
}

View File

@ -2,3 +2,7 @@ pub mod add;
pub mod edit; pub mod edit;
pub mod remove; pub mod remove;
pub mod ls; pub mod ls;
pub mod env_settings;
pub mod utils;

3
src/cli/utils.rs Normal file
View File

@ -0,0 +1,3 @@
pub fn validate_name(name: &str) -> bool{
name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '-')
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/29 21:22:17 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")] #[tokio::main(flavor = "current_thread")]
async fn main() -> io::Result<()> { async fn main() -> Result<(), String> {
let arg = cli().get_matches(); let arg = cli().get_matches();
let pool = get_connection_pool(); let pool = get_connection_pool();
match arg.subcommand() { match arg.subcommand() {
Some(("serverMode", _)) => { mc_socket_listen(pool).await?; }, Some(("serverMode", _)) => { mc_socket_listen(pool).await.map_err(|_| "socket error".to_string())?; },
Some(("add", sub_matches)) => { add::subcommand(sub_matches); }, Some(("add", sub_matches)) => { add::subcommand(sub_matches)?; },
_ => {panic!("subcommand not implemented")} _ => {panic!("subcommand not implemented")}
} }