Added the ability to update mods

This commit is contained in:
xeovalyte 2024-06-03 16:28:49 +02:00
parent 846b3d0edf
commit dac3092502
No known key found for this signature in database
5 changed files with 78 additions and 1 deletions

View File

@ -24,4 +24,10 @@ pub enum Commands {
/// Remove a mod from the modpack
Remove,
/// Update a mod or all mods
Update,
/// Migrate to a other Minecraft version
Migrate,
}

View File

@ -2,3 +2,4 @@ pub mod add;
pub mod init;
pub mod list;
pub mod remove;
pub mod update;

67
src/commands/update.rs Normal file
View File

@ -0,0 +1,67 @@
use crate::{
config,
modrinth::{project::get_project_versions, versions::ProjectVersion},
pack::{Mod, Modpack},
};
use colored::*;
use inquire::MultiSelect;
use std::fmt;
struct Diff {
r#mod: Mod,
new_version: ProjectVersion,
}
impl fmt::Display for Diff {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} {} -> {}",
self.r#mod.title.bold(),
self.r#mod.version.as_ref().unwrap().version_number.red(),
self.new_version.version_number.green()
)
}
}
pub async fn update() {
let mut config = config::Config::get().unwrap();
let modpack = Modpack::from_config(&config).await.unwrap();
let mut diffs: Vec<Diff> = vec![];
// Find the newest compatible version of the mod
for (project_id, r#mod) in modpack.mods {
let project_versions = get_project_versions(&project_id, &config.game_version)
.await
.unwrap();
let new_version = project_versions.get(0).unwrap();
if r#mod.version.as_ref().unwrap().version_number == new_version.version_number {
continue;
}
diffs.push(Diff {
r#mod,
new_version: new_version.clone(),
})
}
if diffs.len() == 0 {
return println!("There are no mods left to update :)");
}
let updated_diffs = MultiSelect::new("Select mods to update", diffs)
.prompt()
.unwrap();
// Bumb the new versions to the config
for diff in updated_diffs {
config
.projects
.insert(diff.r#mod.project_id, diff.new_version.id.to_string());
}
config.save().unwrap();
}

View File

@ -23,6 +23,9 @@ async fn main() {
cli::Commands::List => {
commands::list::list().await;
}
cli::Commands::Update => {
commands::update::update().await;
}
_ => (),
};
}

View File

@ -4,7 +4,7 @@ use serde::Deserialize;
type Response = Vec<ProjectVersion>;
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct ProjectVersion {
pub name: String,
pub version_number: String,