add edit, ls and remove cli commands

This commit is contained in:
2026-06-06 17:20:15 +02:00
parent 25d7a6ffd1
commit e162ef10a3
7 changed files with 164 additions and 16 deletions

View File

@ -1,6 +1,8 @@
use clap::{ Arg, ArgMatches, ArgAction};
use std::collections::HashMap;
pub fn get_args() -> [Arg; 22] {
use clap::{ Arg, ArgAction, ArgMatches, parser::ValueSource};
pub fn get_args() -> [Arg; 21] {
[
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"),
@ -14,7 +16,7 @@ pub fn get_args() -> [Arg; 22] {
Arg::new("WHITELIST").short('w').long("whitelist").help("comma separated list of the playsrs to whitelist (disabled if not provied)"),
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),
// Arg::new("CUSTOM_SERVER_PROPERTIES").long("custom-property").help("add a custom property").action(ArgAction::Append),
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"),
@ -27,7 +29,7 @@ pub fn get_args() -> [Arg; 22] {
]
}
pub fn args_to_env(arguments: &ArgMatches) -> String {
pub fn args_to_settings(arguments: &ArgMatches, default: bool) -> Vec<(String, 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"];
@ -36,14 +38,20 @@ pub fn args_to_env(arguments: &ArgMatches) -> String {
];
for setting in default_handle {
if default == false && arguments.value_source(setting) == Some(ValueSource::DefaultValue) {
continue ;
}
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 default == true || arguments.value_source("FORGE_VERSION") != Some(ValueSource::DefaultValue) {
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") {
@ -51,15 +59,42 @@ pub fn args_to_env(arguments: &ArgMatches) -> 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));
}
// 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));
// }
res
}
// println!("\n\n\narguments : {:?}\nignored:{:?}", arguments, ignored);
res.iter()
pub fn settings_to_env(settings: Vec<(String, String)>) -> String {
settings.iter()
.map(|x| format!("{}={}", x.0, x.1))
.collect::<Vec<String>>().join("\n")
}
pub fn args_to_env(arguments: &ArgMatches, default: bool) -> String {
settings_to_env(args_to_settings(arguments, default))
}
pub fn env_to_settings(env: String) -> Vec<(String, String)> {
env.split("\n")
.filter_map(|s| s.split_once("=")
.map(|(x, y)| (x.to_string(), y.to_string())))
.collect::<Vec<(String, String)>>()
}
pub fn update_env(arguments: &ArgMatches, prev_env: String) -> String{
println!("env : {}" , prev_env);
let settings = env_to_settings(prev_env);
let new_settings = args_to_settings(arguments, false);
let merged: Vec<(String, String)> = settings.into_iter()
.chain(new_settings.into_iter())
.collect::<HashMap<String, String>>()
.into_iter()
.collect();
settings_to_env(merged)
}
// START COMMAND :
// docker run --rm --env-file ./env -u 1000:100 -it --volume $PWD/server:/data itzg/minecraft-server