feat: fabric server jar download feature during init
This commit is contained in:
parent
a19ccc9303
commit
f45493737a
3 changed files with 92 additions and 3 deletions
68
src/main.rs
68
src/main.rs
|
|
@ -1,7 +1,8 @@
|
|||
mod models;
|
||||
mod utils;
|
||||
|
||||
use crate::{models::vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}, utils::{with_progressbar, with_spinner}};
|
||||
use crate::{models::{vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}}, utils::{with_progressbar, with_spinner}};
|
||||
use crate::models::fabric::FabricVersions;
|
||||
use clap::{
|
||||
Parser, Subcommand,
|
||||
builder::styling::{self},
|
||||
|
|
@ -9,7 +10,7 @@ use clap::{
|
|||
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
|
||||
use futures_util::StreamExt;
|
||||
use sha1::{Digest, Sha1};
|
||||
use std::error::Error;
|
||||
use std::{error::Error, fmt::format};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
fn styles() -> styling::Styles {
|
||||
|
|
@ -113,7 +114,68 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||
Ok(())
|
||||
}).await?;
|
||||
}
|
||||
1 => {}
|
||||
1 => {
|
||||
const URL: &str = "https://meta.fabricmc.net/v2/versions";
|
||||
let res = with_spinner("", "", || async {
|
||||
let res: FabricVersions = reqwest::get(URL).await?.json().await?;
|
||||
Ok(res)
|
||||
}).await?;
|
||||
|
||||
let game_version_names = res.game
|
||||
.iter()
|
||||
.filter(|game| game.stable)
|
||||
.map(|game| game.version.clone())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
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::<Vec<_>>();
|
||||
|
||||
let loader_version_index = FuzzySelect::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Select Loader Version")
|
||||
.items(&loader_version_names)
|
||||
.default(0)
|
||||
.interact()?;
|
||||
|
||||
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()
|
||||
.find_map(|installer| {
|
||||
if installer.stable { Some(installer.version.clone()) }
|
||||
else { None }
|
||||
}).ok_or("No Installer Version Found")?;
|
||||
|
||||
let start_msg = format!(
|
||||
"downloading server.jar for {} {}, {}",
|
||||
loaders[loader_index], loader_version_names[loader_version_index],
|
||||
game_version_names[game_version_index],
|
||||
);
|
||||
with_spinner(&start_msg, "download finished", || async {
|
||||
let download_url = format!(
|
||||
"{}/loader/{}/{}/{}/server/jar",
|
||||
URL, game_version, loader_version, installer_version
|
||||
);
|
||||
let res = reqwest::get(download_url).await?;
|
||||
let mut file = tokio::fs::File::create("server.jar").await?;
|
||||
let mut stream = res.bytes_stream();
|
||||
|
||||
while let Some(chunk) = stream.next().await {
|
||||
file.write_all(&chunk?).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}).await?;
|
||||
|
||||
}
|
||||
_ => return Err("Invalid selection".into()),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
src/models/fabric.rs
Normal file
26
src/models/fabric.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct FabricVersions {
|
||||
pub game: Vec<FabricGameVersion>,
|
||||
pub loader: Vec<FabricLoaderVersion>,
|
||||
pub installer: Vec<FabricInstallerVersion>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct FabricGameVersion {
|
||||
pub version: String,
|
||||
pub stable: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct FabricLoaderVersion {
|
||||
pub version: String,
|
||||
pub stable: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct FabricInstallerVersion {
|
||||
pub version: String,
|
||||
pub stable: bool,
|
||||
}
|
||||
|
|
@ -1 +1,2 @@
|
|||
pub mod vanilla;
|
||||
pub mod fabric;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue