Added the 'new' command which creates the scaffolding for a server instance
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Gregory Ballantine 2022-09-19 16:57:10 -04:00
parent 1723c26ba9
commit da099a11ce
3 changed files with 44 additions and 3 deletions

View File

@ -7,3 +7,5 @@ edition = "2021"
[dependencies]
clap = { version = "3.2", features = ["derive"] }
reqwest = { version = "0.11", features = ["blocking"] }
shellexpand = "2.1"

View File

@ -1,3 +1,37 @@
pub fn new_command(server_name: &str, minecraft_version: &str) {
extern crate reqwest;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::io;
use std::io::prelude::*;
pub fn new_command(server_name: &str, minecraft_version: &str) -> Result<(), Box<dyn std::error::Error>> {
println!("Creating new server with name '{}' using version '{}'.", server_name, minecraft_version);
let home_path = shellexpand::tilde("~");
let server_directory_path = format!("{}/{}", home_path, server_name);
let server_file_path = format!("{}/server_{}.jar", server_directory_path, minecraft_version);
// create the server directory
println!("Creating server directory {}.", server_directory_path);
fs::create_dir(&server_directory_path)?;
// download the Minecraft server JAR file
let server_jar_url = "https://piston-data.mojang.com/v1/objects/f69c284232d7c7580bd89a5a4931c3581eae1378/server.jar";
println!("Downloading {} to {}.", server_jar_url, server_file_path);
let mut resp = reqwest::blocking::get(server_jar_url)?;
let mut out = fs::File::create(server_file_path)?;
io::copy(&mut resp, &mut out)?;
// create the start.sh shell script
let script_file_path = format!("{}/start.sh", server_directory_path);
println!("Creating start.sh script.");
let mut script_file = fs::File::create(script_file_path)?;
let script_file_contents = format!("#!/bin/sh\n\ncd {}\njava -Xmx2048M -Xms2048M -jar server_{}.jar nogui", server_directory_path, minecraft_version);
script_file.write_all(script_file_contents.as_bytes())?;
// set the file permissions on the start.sh script
script_file.set_permissions(fs::Permissions::from_mode(0o755))?;
// return empty result to signify everything is okay
Ok(())
}

View File

@ -27,7 +27,12 @@ fn main() {
let cli = Cli::parse();
// map subcommands back to the main command
match &cli.command {
let res = match &cli.command {
Commands::New { server_name, minecraft_version } => cmd::new::new_command(&server_name, &minecraft_version),
}
};
match res {
Ok(_) => {},
Err(e) => panic!("MCST ran into an error: {}", e),
};
}