use colored::Colorize;
use std::collections::HashMap;
use std::io::{self};
use std::path::Path;
mod properties;
const DEFAULT_PROPERTIES_PATH: &str = "server.properties";
fn main() -> io::Result<()> {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
println!(
"{} {} {}",
"Usage:".red(),
args[0].red().bold(),
"[v/property=value ...]".red().bold()
);
println!("{}", "Examples:".cyan());
println!(
" {} {}",
args[0].green(),
"difficulty=hard gamemode=survival".green()
);
println!(
" {} {} {}",
args[0].green(),
"v".green(),
"# View all current settings".dimmed()
);
return Ok(());
}
let properties_path = DEFAULT_PROPERTIES_PATH;
if args.len() == 2 && args[1] == "v" {
return properties::view_properties(properties_path);
}
let mut updates = HashMap::new();
for arg in args.iter().skip(1) {
if arg == "v" {
continue; }
if let Some((key, value)) = arg.split_once('=') {
updates.insert(key.to_string(), value.to_string());
} else {
println!(
"{} {}",
"Warning: Skipping invalid argument:".red().bold(),
arg.red().bold()
);
}
}
let mut properties = if Path::new(properties_path).exists() {
properties::read_properties(properties_path)?
} else {
println!(
"{}",
"No existing server.properties found. Creating a new one...".cyan()
);
HashMap::new()
};
for (key, value) in updates {
properties.insert(key.clone(), value.clone());
println!(
"{}{}{}{}",
"Setting ".cyan(),
key.yellow(),
"=",
value.purple()
);
}
properties::write_properties(properties_path, &properties)?;
println!("{}", "Successfully updated server.properties".green());
if args.iter().any(|arg| arg == "v") {
println!("{}", "\nCurrent settings:".cyan());
println!("{}", "----------------------------------".cyan());
properties::display_properties(&properties);
}
Ok(())
}