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,
loader: Option<String>,
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<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?;
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<Config, Box<dyn Error>> {
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<Config, Box<dyn Error>>;
async fn download(create: &Create) -> Result<Config, Box<dyn Error>>;
}
struct VanillaDownloader {}
@ -71,7 +83,7 @@ impl LoaderDownloader for VanillaDownloader {
"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";
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::<Vec<_>>();
let release_version_names = release_versions
let version_names = versions
.iter()
.map(|version| version.id.clone())
.collect::<Vec<_>>();
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<Config, Box<dyn Error>> {
async fn download(create: &Create) -> Result<Config, Box<dyn Error>> {
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::<Vec<_>>();

View file

@ -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<dyn Error>> {
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?;