diff --git a/.gitignore b/.gitignore index 6b39d31..ecc0280 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -.idea/ \ No newline at end of file +.idea/ +servers/ \ No newline at end of file diff --git a/src/cli/executor.rs b/src/cli/executor.rs index 38e17e3..182d856 100644 --- a/src/cli/executor.rs +++ b/src/cli/executor.rs @@ -59,7 +59,7 @@ pub(crate) fn execute(args: Args, mut config: Config, theme: &Theme) -> Result<( let server = config .get_server(&name) .ok_or(Error::ResourceNotFound("Server not found".to_string()))?; - server.run()?; + server.run(false)?; } DJ::Config { action } => { config_cli::manage_config_action(action, &config)?; @@ -94,7 +94,7 @@ fn handle_server_action( let server = config .get_server(&name) .ok_or(Error::ResourceNotFound("Server not found".to_string()))?; - server.run()?; + server.run(false)?; } ServerAction::Delete { name } => { let server = config diff --git a/src/main.rs b/src/main.rs index 4ebbfff..b6df1f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod error; mod minecraft; mod themes; mod utils; +mod tests; use cli::constructor; use cli::executor; diff --git a/src/minecraft/jars.rs b/src/minecraft/jars.rs index 6059b1b..d2911b7 100644 --- a/src/minecraft/jars.rs +++ b/src/minecraft/jars.rs @@ -122,7 +122,7 @@ impl Jar { let response = reqwest::blocking::get(url)?; let lower_name = self.name.to_lowercase(); let mut builds: Vec; - if &lower_name == "paper" { + if &lower_name == "paper" || &lower_name == "waterfall" || &lower_name == "velocity" { let mut body = response.json::()?; builds = body.get_builds()?; } else if &lower_name == "purpur" { diff --git a/src/minecraft/server.rs b/src/minecraft/server.rs index 9941ced..ad7cafd 100644 --- a/src/minecraft/server.rs +++ b/src/minecraft/server.rs @@ -50,7 +50,7 @@ impl Server { version, build, location: location.to_path_buf(), - gui: false, + gui: true, xms: None, xmx: None, }; @@ -90,7 +90,7 @@ impl Server { ); } - pub fn run(&self) -> Result<(), Error> { + pub fn run(&self, accept_eula: bool) -> Result<(), Error> { let server_info = self.clone(); self.print_info(); println!( @@ -153,13 +153,22 @@ impl Server { for line in reader.lines() { let text = line.unwrap(); if text.contains("You need to agree to the EULA in order to run the server. Go to eula.txt for more info.") { + if accept_eula { + self.accept_eula(); + let server_clone = self.clone(); + let server_copy = thread::spawn(move || server_clone.run(false)); // Create + // thread so we can get "out" of the loop + server_copy.join().unwrap()?; + break; + } let input = read_line("🚨 EULA not accepted! Would you like to accept? (y/n)")?.to_lowercase(); if input == "y" { println!("🛑 Stopping server"); process.kill().expect("Failed to kill child"); self.accept_eula(); let server_clone = self.clone(); - let server_copy = thread::spawn(move || server_clone.run()); // Create thread so we can get "out" of the loop + let server_copy = thread::spawn(move || server_clone.run(false)); // Create + // thread so we can get "out" of the loop server_copy.join().unwrap()?; break; } diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..38f7053 --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,56 @@ +#[cfg(test)] +mod tests { + use std::fs; + use crate::error::Error; + use crate::minecraft::jars; + + #[test] + fn create_paper_server() -> Result<(), Error> { + create_server("paper", "paper_test") + } + + #[test] + fn create_purpur_server() -> Result<(), Error> { + create_server("purpur", "purpur_test") + } + + //TODO: Make sure velocity and waterfall exit properly + + #[test] + fn create_velocity_server() -> Result<(), Error> { + create_server("velocity", "velocity_test") + } + + #[test] + fn create_waterfall_server() -> Result<(), Error> { + create_server("waterfall", "waterfall_test") + } + + fn create_server(jar_name: &str, server_name: &str) -> Result<(), Error> { + let jar_info = jars::load()?; + let jar = jar_info.get_jar(jar_name).unwrap(); + + let current_dir = std::env::current_dir()?; + let server_location = current_dir.join("servers").join(server_name); + fs::create_dir_all(&server_location)?; + + let version = jar.get_versions()?; + let version = version.first().unwrap().to_string(); + let build = jar.get_latest_build(&version).unwrap().to_string(); + let server = jar.download(&version, &build, server_name, &server_location)?; + + assert_eq!(server.server_name, server_name); + + server.run(true)?; + + let plugins_dir = server_location.join("plugins"); + + assert!(plugins_dir.exists()); + + server.delete(); + + assert!(!server_location.exists()); + + Ok(()) + } +} \ No newline at end of file