From 1bd3a23e809581d88b9506bbdf2bbbf1f61ccff2 Mon Sep 17 00:00:00 2001 From: RafayAhmad7548 Date: Mon, 31 Aug 2026 09:21:54 +0500 Subject: [PATCH] feat: add show snapshot/beta/alpha flags to show said versions --- src/commands/create.rs | 45 +++++++++++++++++++++++++++++------------- src/main.rs | 27 +++++++++++++++++++++---- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/commands/create.rs b/src/commands/create.rs index cfd2ebb..f4ade4e 100644 --- a/src/commands/create.rs +++ b/src/commands/create.rs @@ -18,12 +18,21 @@ pub struct Create { // project_name: String, loader: Option, config_file: File, + + show_snapshots: bool, + show_betas: bool, + show_alphas: bool, } impl Create { const LOADERS: [&str; 2] = ["Vanilla", "Fabric"]; - pub async fn new(project_name: String) -> Result> { + pub async fn new( + project_name: String, + show_snapshots: bool, + show_betas: bool, + show_alphas: bool, + ) -> Result> { tokio::fs::create_dir_all(&project_name).await?; std::env::set_current_dir(&project_name)?; let config_file = tokio::fs::File::create("mcsm.toml").await?; @@ -31,6 +40,9 @@ impl Create { /*project_name,*/ loader: None, config_file, + show_snapshots, + show_betas, + show_alphas, }) } @@ -46,8 +58,8 @@ impl Create { pub async fn download_server(&self) -> Result> { Ok(match self.loader.as_deref() { - Some("Vanilla") => VanillaDownloader::download().await?, - Some("Fabric") => FabricDownloader::download().await?, + Some("Vanilla") => VanillaDownloader::download(self).await?, + Some("Fabric") => FabricDownloader::download(self).await?, _ => return Err("Invalid selection".into()), }) } @@ -62,7 +74,7 @@ impl Create { trait LoaderDownloader { fn name() -> String; - async fn download() -> Result>; + async fn download(create: &Create) -> Result>; } struct VanillaDownloader {} @@ -71,7 +83,7 @@ impl LoaderDownloader for VanillaDownloader { "Vanilla".to_string() } - async fn download() -> Result> { + async fn download(create: &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?; @@ -79,25 +91,30 @@ impl LoaderDownloader for VanillaDownloader { }) .await?; - let release_versions = res + let versions = res .versions .iter() - .filter(|version| version.version_type == VanillaVersionType::Release) + .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 release_version_names = release_versions + let version_names = versions .iter() .map(|version| version.id.clone()) .collect::>(); let version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) .with_prompt("Select Version") - .items(&release_version_names) + .items(&version_names) .default(0) .interact()?; let version_detail: VanillaVersionDetail = with_spinner("", "", || async { let version_detail: VanillaVersionDetail = - reqwest::get(release_versions[version_index].url.clone()) + reqwest::get(versions[version_index].url.clone()) .await? .json() .await?; @@ -108,7 +125,7 @@ impl LoaderDownloader for VanillaDownloader { let start_msg = format!( "downloading server.jar for {} {}", Self::name(), - release_version_names[version_index] + version_names[version_index] ); with_progressbar( &start_msg, @@ -146,7 +163,7 @@ impl LoaderDownloader for VanillaDownloader { let config = Config { server: Server { - version: release_version_names[version_index].to_string(), + version: version_names[version_index].to_string(), loader: Self::name(), loader_version: None, }, @@ -163,7 +180,7 @@ impl LoaderDownloader for FabricDownloader { "Fabric".to_string() } - async fn download() -> Result> { + async fn download(create: &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?; @@ -174,7 +191,7 @@ impl LoaderDownloader for FabricDownloader { let game_version_names = res .game .iter() - .filter(|game| game.stable) + .filter(|game| create.show_snapshots || game.stable) .map(|game| game.version.clone()) .collect::>(); diff --git a/src/main.rs b/src/main.rs index 45e0cd6..28209df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,8 +25,22 @@ struct Mcsm { #[derive(Subcommand)] enum Commands { - /// create a new minecraft server - Create { project_name: String }, + /// Create a new minecraft server + Create { + project_name: String, + + /// Show snapshot versions + #[arg(long)] + show_snapshots: bool, + + /// Show old beta versions + #[arg(long)] + show_betas: bool, + /// + /// Show old alpha versions + #[arg(long)] + show_alphas: bool, + }, } #[tokio::main] @@ -34,8 +48,13 @@ async fn main() -> Result<(), Box> { let mcsm = Mcsm::parse(); match mcsm.command { - Commands::Create { project_name } => { - let mut create = Create::new(project_name.clone()).await?; + Commands::Create { project_name, show_snapshots, show_betas, show_alphas } => { + let mut create = Create::new( + project_name, + show_snapshots, + show_betas, + show_alphas, + ).await?; create.init_loader().await?; let config = create.download_server().await?; create.save_config(config).await?;