feat: add show snapshot/beta/alpha flags to show said versions

This commit is contained in:
RafayAhmad7548 2026-08-31 09:21:54 +05:00
parent 8bf56d2c03
commit 1bd3a23e80
Signed by: RafayAhmad
SSH key fingerprint: SHA256:WURX8viobA1uawb4dWM3LqYrY+XPcZcXhAXAlrYdhtE
2 changed files with 54 additions and 18 deletions

View file

@ -18,12 +18,21 @@ pub struct Create {
// project_name: String, // project_name: String,
loader: Option<String>, loader: Option<String>,
config_file: File, config_file: File,
show_snapshots: bool,
show_betas: bool,
show_alphas: bool,
} }
impl Create { impl Create {
const LOADERS: [&str; 2] = ["Vanilla", "Fabric"]; const LOADERS: [&str; 2] = ["Vanilla", "Fabric"];
pub async fn new(project_name: String) -> Result<Self, Box<dyn Error>> { pub async fn new(
project_name: String,
show_snapshots: bool,
show_betas: bool,
show_alphas: bool,
) -> Result<Self, Box<dyn Error>> {
tokio::fs::create_dir_all(&project_name).await?; tokio::fs::create_dir_all(&project_name).await?;
std::env::set_current_dir(&project_name)?; std::env::set_current_dir(&project_name)?;
let config_file = tokio::fs::File::create("mcsm.toml").await?; let config_file = tokio::fs::File::create("mcsm.toml").await?;
@ -31,6 +40,9 @@ impl Create {
/*project_name,*/ /*project_name,*/
loader: None, loader: None,
config_file, config_file,
show_snapshots,
show_betas,
show_alphas,
}) })
} }
@ -46,8 +58,8 @@ impl Create {
pub async fn download_server(&self) -> Result<Config, Box<dyn Error>> { pub async fn download_server(&self) -> Result<Config, Box<dyn Error>> {
Ok(match self.loader.as_deref() { Ok(match self.loader.as_deref() {
Some("Vanilla") => VanillaDownloader::download().await?, Some("Vanilla") => VanillaDownloader::download(self).await?,
Some("Fabric") => FabricDownloader::download().await?, Some("Fabric") => FabricDownloader::download(self).await?,
_ => return Err("Invalid selection".into()), _ => return Err("Invalid selection".into()),
}) })
} }
@ -62,7 +74,7 @@ impl Create {
trait LoaderDownloader { trait LoaderDownloader {
fn name() -> String; fn name() -> String;
async fn download() -> Result<Config, Box<dyn Error>>; async fn download(create: &Create) -> Result<Config, Box<dyn Error>>;
} }
struct VanillaDownloader {} struct VanillaDownloader {}
@ -71,7 +83,7 @@ impl LoaderDownloader for VanillaDownloader {
"Vanilla".to_string() "Vanilla".to_string()
} }
async fn download() -> Result<Config, Box<dyn Error>> { async fn download(create: &Create) -> Result<Config, Box<dyn Error>> {
const URL: &str = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; const URL: &str = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
let res = with_spinner("", "", || async { let res = with_spinner("", "", || async {
let res: VanillaVersions = reqwest::get(URL).await?.json().await?; let res: VanillaVersions = reqwest::get(URL).await?.json().await?;
@ -79,25 +91,30 @@ impl LoaderDownloader for VanillaDownloader {
}) })
.await?; .await?;
let release_versions = res let versions = res
.versions .versions
.iter() .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::<Vec<_>>(); .collect::<Vec<_>>();
let release_version_names = release_versions let version_names = versions
.iter() .iter()
.map(|version| version.id.clone()) .map(|version| version.id.clone())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let version_index = FuzzySelect::with_theme(&ColorfulTheme::default()) let version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt("Select Version") .with_prompt("Select Version")
.items(&release_version_names) .items(&version_names)
.default(0) .default(0)
.interact()?; .interact()?;
let version_detail: VanillaVersionDetail = with_spinner("", "", || async { let version_detail: VanillaVersionDetail = with_spinner("", "", || async {
let version_detail: VanillaVersionDetail = let version_detail: VanillaVersionDetail =
reqwest::get(release_versions[version_index].url.clone()) reqwest::get(versions[version_index].url.clone())
.await? .await?
.json() .json()
.await?; .await?;
@ -108,7 +125,7 @@ impl LoaderDownloader for VanillaDownloader {
let start_msg = format!( let start_msg = format!(
"downloading server.jar for {} {}", "downloading server.jar for {} {}",
Self::name(), Self::name(),
release_version_names[version_index] version_names[version_index]
); );
with_progressbar( with_progressbar(
&start_msg, &start_msg,
@ -146,7 +163,7 @@ impl LoaderDownloader for VanillaDownloader {
let config = Config { let config = Config {
server: Server { server: Server {
version: release_version_names[version_index].to_string(), version: version_names[version_index].to_string(),
loader: Self::name(), loader: Self::name(),
loader_version: None, loader_version: None,
}, },
@ -163,7 +180,7 @@ impl LoaderDownloader for FabricDownloader {
"Fabric".to_string() "Fabric".to_string()
} }
async fn download() -> Result<Config, Box<dyn Error>> { async fn download(create: &Create) -> Result<Config, Box<dyn Error>> {
const URL: &str = "https://meta.fabricmc.net/v2/versions"; const URL: &str = "https://meta.fabricmc.net/v2/versions";
let res = with_spinner("", "", || async { let res = with_spinner("", "", || async {
let res: FabricVersions = reqwest::get(URL).await?.json().await?; let res: FabricVersions = reqwest::get(URL).await?.json().await?;
@ -174,7 +191,7 @@ impl LoaderDownloader for FabricDownloader {
let game_version_names = res let game_version_names = res
.game .game
.iter() .iter()
.filter(|game| game.stable) .filter(|game| create.show_snapshots || game.stable)
.map(|game| game.version.clone()) .map(|game| game.version.clone())
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View file

@ -25,8 +25,22 @@ struct Mcsm {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
/// create a new minecraft server /// Create a new minecraft server
Create { project_name: String }, 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] #[tokio::main]
@ -34,8 +48,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mcsm = Mcsm::parse(); let mcsm = Mcsm::parse();
match mcsm.command { match mcsm.command {
Commands::Create { project_name } => { Commands::Create { project_name, show_snapshots, show_betas, show_alphas } => {
let mut create = Create::new(project_name.clone()).await?; let mut create = Create::new(
project_name,
show_snapshots,
show_betas,
show_alphas,
).await?;
create.init_loader().await?; create.init_loader().await?;
let config = create.download_server().await?; let config = create.download_server().await?;
create.save_config(config).await?; create.save_config(config).await?;