diff --git a/Cargo.lock b/Cargo.lock index 8c825c1..f97624d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -48,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -97,6 +97,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -131,11 +141,18 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "mcserver" version = "0.1.0" dependencies = [ "clap", + "colored", "serde", "toml", ] @@ -256,13 +273,37 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -271,28 +312,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -305,24 +364,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index b584212..c592399 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" [dependencies] clap = { version = "4.5.20", features = ["derive"] } +colored = "2.1.0" serde = { version = "1.0.210", features = ["derive"] } toml = "0.8.19" diff --git a/src/config.rs b/src/config.rs index 173d4bd..430985a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,23 +1,178 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; +use std::fs; +use std::io::Error as IoError; +use toml; + #[derive(Serialize, Deserialize, Debug)] -struct Config { - mcserver: McServer, - server: Server, +struct ConfigToml { + mcserver: Option, + server: Option, } #[derive(Serialize, Deserialize, Debug)] -struct McServer { - project_name: String, - logfile: String, - tunnel: String, - java: String, +struct ConfigTomlMcServer { + project_name: Option, + logfile: Option, + tunnel: Option, + java: Option, } #[derive(Serialize, Deserialize, Debug)] -struct Server { - online_mode: bool, - version: String, - server_type: String, - category: String, - providor: String, +struct ConfigTomlServer { + online_mode: Option, + version: Option, + server_type: Option, + category: Option, + providor: Option, + url: Option, +} + +#[derive(Debug)] +#[allow(dead_code)] +pub struct Config { + pub project_name: String, + pub logfile: String, + pub tunnel: String, + pub java: String, + pub online_mode: bool, + pub version: String, + pub server_type: String, + pub category: String, + pub providor: String, + pub url: String, +} + + + +impl Config { + pub fn new(project_name: &str) -> Self { + let f1 = format!("./{}/config.toml", project_name); + let f2 = format!("./{}/config.toml", project_name); + + let config_filepaths: [&str; 2] = [&f1, &f2]; + println!("{:?}", config_filepaths); + let mut content: String = "".to_owned(); + + for filepath in &config_filepaths { + let result: Result = fs::read_to_string(filepath); + + if result.is_ok() { + content = result.unwrap(); + break; + } + } + + let config_toml: ConfigToml = toml::from_str(&content).unwrap_or_else(|_| { + println!("Failed to create ConfigToml Object out of config file."); + ConfigToml { + mcserver: None, + server: None, + } + }); + + let (project_name, logfile, tunnel, java): (String, String, String, String) = + match config_toml.mcserver { + Some(mcserver) => { + let mc_project_name: String = mcserver.project_name.unwrap_or_else(|| { + println!("Missing field project_name in table mcserver."); + "unknown".to_owned() + }); + let mc_logfile: String = mcserver.logfile.unwrap_or_else(|| { + println!("Missing field logfile in table mcserver."); + "unknown".to_owned() + }); + + let mc_tunnel: String = mcserver.tunnel.unwrap_or_else(|| { + println!("Missing field tunnel in table mcserver."); + "unknown".to_owned() + }); + + let mc_java: String = mcserver.java.unwrap_or_else(|| { + println!("Missing field java in table mcserver."); + "unknown".to_owned() + }); + (mc_project_name, mc_logfile, mc_tunnel, mc_java) + } + None => { + println!("Missing table mcserver."); + ( + "unknown".to_owned(), + "unknown".to_owned(), + "unknown".to_owned(), + "unknown".to_owned(), + ) + } + }; + + let (online_mode, version, server_type, category, providor, url): ( + bool, + String, + String, + String, + String, + String, + ) = match config_toml.server { + Some(server) => { + let srv_online_mode: bool = server.online_mode.unwrap_or_else(|| { + println!("Missing field online_mode in table server."); + false + }); + + let srv_version: String = server.version.unwrap_or_else(|| { + println!("Missing field version in table server."); + "unknown".to_owned() + }); + + let srv_type: String = server.server_type.unwrap_or_else(|| { + println!("Missing field type in table server."); + "unknown".to_owned() + }); + let srv_category: String = server.category.unwrap_or_else(|| { + println!("Missing field type in table server."); + "unknown".to_owned() + }); + let srv_providor: String = server.providor.unwrap_or_else(|| { + println!("Missing field type in table server."); + "unknown".to_owned() + }); + + let srv_url: String = server.url.unwrap_or_else(|| { + println!("Missing field url in table server."); + "unknown".to_owned() + }); + ( + srv_online_mode, + srv_version, + srv_type, + srv_category, + srv_providor, + srv_url, + ) + } + None => { + println!("Missing table server."); + ( + false, + "unknown".to_owned(), + "unknown".to_owned(), + "unknown".to_owned(), + "unknown".to_owned(), + "unknown".to_owned(), + ) + } + }; + + Config { + project_name, + logfile, + tunnel, + java, + online_mode, + version, + server_type, + category, + providor, + url, + } + } } \ No newline at end of file diff --git a/src/setup.rs b/src/setup.rs index 0b6adf2..21c2f42 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -4,12 +4,16 @@ use std::{ panic, path::Path, }; + +use crate::config::Config; pub fn setup() { let project_name = take_project_name(); let project_name = project_name.as_str(); let _ = make_project_directory(project_name); make_project_tree(project_name); make_empty_config(project_name); + let config = Config::new(project_name); + println!("{:?}",config); } fn take_project_name() -> String {