From 10920719681f585d6a67fc2235c57e4a9c02fd3c Mon Sep 17 00:00:00 2001 From: RafayAhmad7548 Date: Mon, 31 Aug 2026 10:39:01 +0500 Subject: [PATCH] feat(create): add args to specify minecraft version, loader & loader version --- src/commands/create.rs | 160 ++++++++++++++++++++++++++--------------- src/main.rs | 34 +++++++-- 2 files changed, 132 insertions(+), 62 deletions(-) diff --git a/src/commands/create.rs b/src/commands/create.rs index f4ade4e..5b39f73 100644 --- a/src/commands/create.rs +++ b/src/commands/create.rs @@ -16,9 +16,12 @@ use crate::{ pub struct Create { // project_name: String, - loader: Option, config_file: File, + version: Option, + loader: Option, + loader_version: Option, + show_snapshots: bool, show_betas: bool, show_alphas: bool, @@ -29,6 +32,9 @@ impl Create { pub async fn new( project_name: String, + version: Option, + loader: Option, + loader_version: Option, show_snapshots: bool, show_betas: bool, show_alphas: bool, @@ -38,8 +44,10 @@ impl Create { let config_file = tokio::fs::File::create("mcsm.toml").await?; Ok(Create { /*project_name,*/ - loader: None, config_file, + version, + loader, + loader_version, show_snapshots, show_betas, show_alphas, @@ -56,11 +64,12 @@ impl Create { Ok(()) } - pub async fn download_server(&self) -> Result> { + pub async fn download_server(&mut self) -> Result> { Ok(match self.loader.as_deref() { Some("Vanilla") => VanillaDownloader::download(self).await?, Some("Fabric") => FabricDownloader::download(self).await?, - _ => return Err("Invalid selection".into()), + Some(l) => return Err(format!("Invalid loader specified: {}", l).into()), + None => return Err("No loader specified".into()), }) } @@ -74,7 +83,7 @@ impl Create { trait LoaderDownloader { fn name() -> String; - async fn download(create: &Create) -> Result>; + async fn download(create: &mut Create) -> Result>; } struct VanillaDownloader {} @@ -83,7 +92,7 @@ impl LoaderDownloader for VanillaDownloader { "Vanilla".to_string() } - async fn download(create: &Create) -> Result> { + async fn download(create: &mut Create) -> Result> { const URL: &str = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; let res = with_spinner("", "", || async { let res: VanillaVersions = reqwest::get(URL).await?.json().await?; @@ -91,30 +100,47 @@ impl LoaderDownloader for VanillaDownloader { }) .await?; - let versions = res + if let Some(version) = create.version.clone() { + let version_exists = res + .versions + .iter() + .any(|v| v.id == version); + if !version_exists { return Err("Invalid minecraft version specified".into()) } + } + else { + let versions = res + .versions + .iter() + .filter(|version| { + version.version_type == VanillaVersionType::Release || + (create.show_snapshots && version.version_type == VanillaVersionType::Snapshot) || + (create.show_betas && version.version_type == VanillaVersionType::OldBeta) || + (create.show_alphas && version.version_type == VanillaVersionType::OldAlpha) + }) + .collect::>(); + let version_names = versions + .iter() + .map(|version| version.id.as_str()) + .collect::>(); + + let version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) + .with_prompt("Select Version") + .items(&version_names) + .default(0) + .interact()?; + + create.version = Some(version_names[version_index].to_string()); + } + + let version = res .versions .iter() - .filter(|version| { - version.version_type == VanillaVersionType::Release || - (create.show_snapshots && version.version_type == VanillaVersionType::Snapshot) || - (create.show_betas && version.version_type == VanillaVersionType::OldBeta) || - (create.show_alphas && version.version_type == VanillaVersionType::OldAlpha) - }) - .collect::>(); - let version_names = versions - .iter() - .map(|version| version.id.clone()) - .collect::>(); - - let version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) - .with_prompt("Select Version") - .items(&version_names) - .default(0) - .interact()?; + .find(|v| &v.id == create.version.as_ref().unwrap()) + .unwrap(); let version_detail: VanillaVersionDetail = with_spinner("", "", || async { let version_detail: VanillaVersionDetail = - reqwest::get(versions[version_index].url.clone()) + reqwest::get(version.url.clone()) .await? .json() .await?; @@ -125,7 +151,7 @@ impl LoaderDownloader for VanillaDownloader { let start_msg = format!( "downloading server.jar for {} {}", Self::name(), - version_names[version_index] + create.version.as_ref().unwrap(), ); with_progressbar( &start_msg, @@ -163,7 +189,7 @@ impl LoaderDownloader for VanillaDownloader { let config = Config { server: Server { - version: version_names[version_index].to_string(), + version: create.version.clone().unwrap(), loader: Self::name(), loader_version: None, }, @@ -180,7 +206,7 @@ impl LoaderDownloader for FabricDownloader { "Fabric".to_string() } - async fn download(create: &Create) -> Result> { + async fn download(create: &mut Create) -> Result> { const URL: &str = "https://meta.fabricmc.net/v2/versions"; let res = with_spinner("", "", || async { let res: FabricVersions = reqwest::get(URL).await?.json().await?; @@ -188,34 +214,54 @@ impl LoaderDownloader for FabricDownloader { }) .await?; - let game_version_names = res - .game - .iter() - .filter(|game| create.show_snapshots || game.stable) - .map(|game| game.version.clone()) - .collect::>(); + if let Some(version) = create.version.clone() { + let exists = res + .game + .iter() + .any(|game| game.version == version); + if !exists { return Err("Invalid minecraft version specified".into()) } + } + else { + let game_version_names = res + .game + .iter() + .filter(|game| create.show_snapshots || game.stable) + .map(|game| game.version.as_str()) + .collect::>(); - let game_version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) - .with_prompt("Select Game Version") - .items(&game_version_names) - .default(0) - .interact()?; + let game_version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) + .with_prompt("Select Game Version") + .items(&game_version_names) + .default(0) + .interact()?; - let loader_version_names = res - .loader - .iter() - .filter(|loader| loader.stable) - .map(|loader| loader.version.clone()) - .collect::>(); + create.version = Some(game_version_names[game_version_index].to_string()); + } - let loader_version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) - .with_prompt("Select Loader Version") - .items(&loader_version_names) - .default(0) - .interact()?; + if let Some(loader_version) = create.loader_version.clone() { + let exists = res + .loader + .iter() + .any(|loader| loader.version == loader_version); + if !exists { return Err(format!("Invalid {} loader version specified", Self::name()).into()) } + } + else { + let loader_version_names = res + .loader + .iter() + .filter(|loader| loader.stable) + .map(|loader| loader.version.clone()) + .collect::>(); + + let loader_version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) + .with_prompt("Select Loader Version") + .items(&loader_version_names) + .default(0) + .interact()?; + + create.loader_version = Some(loader_version_names[loader_version_index].to_string()); + } - let game_version = game_version_names[game_version_index].clone(); - let loader_version = loader_version_names[loader_version_index].clone(); let installer_version = res .installer .iter() @@ -231,13 +277,13 @@ impl LoaderDownloader for FabricDownloader { let start_msg = format!( "downloading server.jar for {} {}, {}", Self::name(), - loader_version_names[loader_version_index], - game_version_names[game_version_index], + create.loader_version.as_ref().unwrap(), + create.version.as_ref().unwrap(), ); with_spinner(&start_msg, "download finished", || async { let download_url = format!( "{}/loader/{}/{}/{}/server/jar", - URL, game_version, loader_version, installer_version + URL, create.version.as_ref().unwrap(), create.loader_version.as_ref().unwrap(), installer_version ); let res = reqwest::get(download_url).await?; let mut file = tokio::fs::File::create("server.jar").await?; @@ -253,9 +299,9 @@ impl LoaderDownloader for FabricDownloader { let config = Config { server: Server { - version: game_version, + version: create.version.clone().unwrap(), loader: Self::name(), - loader_version: Some(loader_version), + loader_version: create.loader_version.clone(), }, mods: Mods {}, }; diff --git a/src/main.rs b/src/main.rs index 28209df..4beb546 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,10 +25,22 @@ struct Mcsm { #[derive(Subcommand)] enum Commands { - /// Create a new minecraft server - Create { + /// Create a new minecraft server + Create { project_name: String, + /// Minecraft version + #[arg(long)] + version: Option, + + /// Mod Loader, can be either Vanilla or Fabric + #[arg(long)] + loader: Option, + + /// Mod Loader version, ignored if loader is Vanilla + #[arg(long)] + loader_version: Option, + /// Show snapshot versions #[arg(long)] show_snapshots: bool, @@ -48,14 +60,26 @@ async fn main() -> Result<(), Box> { let mcsm = Mcsm::parse(); match mcsm.command { - Commands::Create { project_name, show_snapshots, show_betas, show_alphas } => { + Commands::Create { + project_name, + version, + loader, + loader_version, + show_snapshots, + show_betas, + show_alphas, + } => { + let loader_is_unset = loader == None; let mut create = Create::new( project_name, + version, + loader, + loader_version, show_snapshots, show_betas, - show_alphas, + show_alphas ).await?; - create.init_loader().await?; + if loader_is_unset { create.init_loader().await? } let config = create.download_server().await?; create.save_config(config).await?; }