feat(install): install feature, installing mods into mods directory
This commit is contained in:
parent
809151b28c
commit
5359b39c9b
7 changed files with 176 additions and 4 deletions
19
Cargo.lock
generated
19
Cargo.lock
generated
|
|
@ -517,6 +517,12 @@ version = "0.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hex"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
|
|
@ -888,10 +894,12 @@ dependencies = [
|
||||||
"colored",
|
"colored",
|
||||||
"dialoguer",
|
"dialoguer",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
"hex",
|
||||||
"indicatif",
|
"indicatif",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"sha1",
|
"sha1",
|
||||||
|
"sha2",
|
||||||
"terminal_size",
|
"terminal_size",
|
||||||
"textwrap",
|
"textwrap",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|
@ -1412,6 +1420,17 @@ dependencies = [
|
||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "446ba717509524cb3f22f17ecc096f10f4822d76ab5c0b9822c5f9c284e825f4"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shell-words"
|
name = "shell-words"
|
||||||
version = "1.1.1"
|
version = "1.1.1"
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,12 @@ clap = { version = "4.6.6", features = ["derive"] }
|
||||||
colored = "3.1.1"
|
colored = "3.1.1"
|
||||||
dialoguer = { version = "0.12.0", features = ["fuzzy-select"] }
|
dialoguer = { version = "0.12.0", features = ["fuzzy-select"] }
|
||||||
futures-util = "0.3.34"
|
futures-util = "0.3.34"
|
||||||
|
hex = "0.4.3"
|
||||||
indicatif = "0.18.6"
|
indicatif = "0.18.6"
|
||||||
reqwest = { version = "0.13.4", features = ["json", "stream", "query"] }
|
reqwest = { version = "0.13.4", features = ["json", "stream", "query"] }
|
||||||
serde = { version = "1.0.229", features = ["derive"] }
|
serde = { version = "1.0.229", features = ["derive"] }
|
||||||
sha1 = "0.11.0"
|
sha1 = "0.11.0"
|
||||||
|
sha2 = "0.11.0"
|
||||||
terminal_size = "0.4.4"
|
terminal_size = "0.4.4"
|
||||||
textwrap = "0.16.2"
|
textwrap = "0.16.2"
|
||||||
tokio = { version = "1.53.1", features = ["full"] }
|
tokio = { version = "1.53.1", features = ["full"] }
|
||||||
|
|
|
||||||
108
src/commands/install.rs
Normal file
108
src/commands/install.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
models::{
|
||||||
|
config::Config,
|
||||||
|
install::{InstallQueryParams, InstallResult},
|
||||||
|
},
|
||||||
|
utils::{verify_sha512, with_progressbar, with_spinner},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Install {
|
||||||
|
mods: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Install {
|
||||||
|
const BASE_URL: &str = "https://api.modrinth.com/v2";
|
||||||
|
|
||||||
|
pub fn new(mods: Vec<String>) -> Self {
|
||||||
|
Install { mods }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(&self) -> Result<(), Box<dyn Error>> {
|
||||||
|
let file_res = tokio::fs::read_to_string("mcsm.toml").await;
|
||||||
|
if file_res.is_err() {
|
||||||
|
return Err("current directory is not a mcsm project".into());
|
||||||
|
}
|
||||||
|
let config: Config = toml::from_str(&file_res.unwrap())?;
|
||||||
|
|
||||||
|
if config.server.loader == "Vanilla" {
|
||||||
|
return Err("cannot search for mods on Vanilla server".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
if std::env::set_current_dir("mods").is_err() {
|
||||||
|
return Err("mods folder not found".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
for mod_name in &self.mods {
|
||||||
|
let url = format!("{}/project/{}/version", Self::BASE_URL, mod_name);
|
||||||
|
|
||||||
|
let params = InstallQueryParams {
|
||||||
|
loaders: format!(r#"["{}"]"#, config.server.loader).to_lowercase(),
|
||||||
|
game_versions: format!(r#"["{}"]"#, config.server.version),
|
||||||
|
};
|
||||||
|
|
||||||
|
let install_result: Vec<InstallResult> = with_spinner(
|
||||||
|
format!("fetching {}", mod_name).as_str(),
|
||||||
|
format!("fetched {}", mod_name).as_str(),
|
||||||
|
|| async {
|
||||||
|
let res = client.get(url).query(¶ms).send().await;
|
||||||
|
|
||||||
|
if res.is_err() {
|
||||||
|
return Err(format!(r#"mod "{}" not found"#, mod_name).into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(res.unwrap().json().await?)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let file = install_result.first().unwrap().files.first().unwrap();
|
||||||
|
with_progressbar(
|
||||||
|
format!("downloading {}", mod_name).as_str(),
|
||||||
|
format!("downloaded {}", mod_name).as_str(),
|
||||||
|
file.size,
|
||||||
|
|pb| async move {
|
||||||
|
let mod_bytes = reqwest::get(&file.url).await?;
|
||||||
|
let mut file = tokio::fs::File::create(&file.filename).await?;
|
||||||
|
let mut stream = mod_bytes.bytes_stream();
|
||||||
|
|
||||||
|
while let Some(chunk) = stream.next().await {
|
||||||
|
let chunk = chunk?;
|
||||||
|
file.write_all(&chunk).await?;
|
||||||
|
pb.inc(chunk.len() as u64);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
with_spinner("verifying sha512 hash", "sha512 hash verified", || async {
|
||||||
|
let data = tokio::fs::read(&file.filename).await?;
|
||||||
|
let verified = verify_sha512(&data, &file.hashes.sha512);
|
||||||
|
|
||||||
|
if !verified {
|
||||||
|
tokio::fs::remove_file("server.jar").await?;
|
||||||
|
return Err("sha1 verifaction failed for downloaded file".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{} finished installing mods",
|
||||||
|
dialoguer::console::style("✔").green()
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod create;
|
pub mod create;
|
||||||
|
pub mod install;
|
||||||
pub mod search;
|
pub mod search;
|
||||||
|
|
|
||||||
23
src/main.rs
23
src/main.rs
|
|
@ -2,7 +2,7 @@ mod commands;
|
||||||
mod models;
|
mod models;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use crate::commands::{create::Create, search::Search};
|
use crate::commands::{create::Create, install::Install, search::Search};
|
||||||
use clap::{
|
use clap::{
|
||||||
Parser, Subcommand,
|
Parser, Subcommand,
|
||||||
builder::styling::{self},
|
builder::styling::{self},
|
||||||
|
|
@ -53,13 +53,13 @@ enum Commands {
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
show_alphas: bool,
|
show_alphas: bool,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Search for mods on modrinth for project in current directory
|
/// Search for mods on modrinth for project in current directory
|
||||||
/// automticallly filters mod loader, minecraft version from config file
|
/// automticallly filters mod loader, minecraft version from config file
|
||||||
Search {
|
Search {
|
||||||
/// Search term to look up, e.g. project name or keyword.
|
/// Search term to look up, e.g. project name or keyword.
|
||||||
query: String,
|
query: String,
|
||||||
|
|
||||||
/// Number of results to skip before returning matches.
|
/// Number of results to skip before returning matches.
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
offset: Option<u32>,
|
offset: Option<u32>,
|
||||||
|
|
@ -68,6 +68,13 @@ enum Commands {
|
||||||
#[arg(long, value_parser = clap::value_parser!(u32).range(1..=100))]
|
#[arg(long, value_parser = clap::value_parser!(u32).range(1..=100))]
|
||||||
limit: Option<u32>,
|
limit: Option<u32>,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/// Install mods from modrinth for project in current directory
|
||||||
|
Install {
|
||||||
|
/// List of mods to install
|
||||||
|
#[arg(trailing_var_arg = true)]
|
||||||
|
mods: Vec<String>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|
@ -101,10 +108,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let config = create.download_server().await?;
|
let config = create.download_server().await?;
|
||||||
create.save_config(config).await?;
|
create.save_config(config).await?;
|
||||||
}
|
}
|
||||||
Commands::Search { query, offset, limit } => {
|
Commands::Search {
|
||||||
|
query,
|
||||||
|
offset,
|
||||||
|
limit,
|
||||||
|
} => {
|
||||||
let search = Search::new(query, offset, limit);
|
let search = Search::new(query, offset, limit);
|
||||||
search.run().await?;
|
search.run().await?;
|
||||||
}
|
}
|
||||||
|
Commands::Install { mods } => {
|
||||||
|
let install = Install::new(mods);
|
||||||
|
install.run().await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
26
src/models/install.rs
Normal file
26
src/models/install.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct InstallQueryParams {
|
||||||
|
pub loaders: String,
|
||||||
|
pub game_versions: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct InstallResult {
|
||||||
|
pub version_number: String,
|
||||||
|
pub files: Vec<InstallFile>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct InstallFile {
|
||||||
|
pub hashes: InstallHashes,
|
||||||
|
pub url: String,
|
||||||
|
pub filename: String,
|
||||||
|
pub size: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct InstallHashes {
|
||||||
|
pub sha512: String,
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod fabric;
|
pub mod fabric;
|
||||||
|
pub mod install;
|
||||||
pub mod search;
|
pub mod search;
|
||||||
pub mod vanilla;
|
pub mod vanilla;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue