feat(install): save installed mods to config after installation

This commit is contained in:
RafayAhmad7548 2026-09-03 13:31:23 +05:00
parent 1352c5f559
commit ea766de5c3
Signed by: RafayAhmad
SSH key fingerprint: SHA256:WURX8viobA1uawb4dWM3LqYrY+XPcZcXhAXAlrYdhtE
3 changed files with 21 additions and 11 deletions

View file

@ -1,4 +1,4 @@
use std::error::Error;
use std::{collections::HashMap, error::Error};
use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme};
use futures_util::StreamExt;
@ -6,7 +6,7 @@ use tokio::{fs::File, io::AsyncWriteExt};
use crate::{
models::{
config::{Config, Mods, Server},
config::{Config, Server},
fabric::FabricVersions,
vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions},
},
@ -188,7 +188,7 @@ impl LoaderDownloader for VanillaDownloader {
loader: Self::name(),
loader_version: None,
},
mods: Mods {},
mods: HashMap::new(),
};
Ok(config)
@ -302,7 +302,7 @@ impl LoaderDownloader for FabricDownloader {
loader: Self::name(),
loader_version: create.loader_version.clone(),
},
mods: Mods {},
mods: HashMap::new(),
};
Ok(config)

View file

@ -27,7 +27,7 @@ impl Install {
if file_res.is_err() {
return Err("current directory is not a mcsm project".into());
}
let config: Config = toml::from_str(&file_res.unwrap())?;
let mut config: Config = toml::from_str(&file_res.unwrap())?;
if config.server.loader == "Vanilla" {
return Err("cannot search for mods on Vanilla server".into());
@ -47,7 +47,7 @@ impl Install {
game_versions: format!(r#"["{}"]"#, config.server.version),
};
let install_result: Vec<InstallResult> = with_spinner(
let res: Vec<InstallResult> = with_spinner(
format!("fetching {}", mod_name).as_str(),
format!("fetched {}", mod_name).as_str(),
|| async {
@ -62,7 +62,9 @@ impl Install {
)
.await?;
let file = install_result.first().unwrap().files.first().unwrap();
let install_result = res.first().unwrap();
let file = &install_result.files.first().unwrap();
let version_number = &install_result.version_number;
with_progressbar(
format!("downloading {}", mod_name).as_str(),
format!("downloaded {}", mod_name).as_str(),
@ -96,6 +98,8 @@ impl Install {
})
.await?;
println!();
config.mods.insert(mod_name.clone(), version_number.clone());
}
println!(
@ -103,6 +107,13 @@ impl Install {
dialoguer::console::style("✔").green()
);
with_spinner("saving to config", "saved to config", || async {
std::env::set_current_dir("..")?;
let config_str = toml::to_string(&config)?;
tokio::fs::write("mcsm.toml", config_str).await?;
Ok(())
}).await?;
Ok(())
}
}

View file

@ -1,9 +1,11 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct Config {
pub server: Server,
pub mods: Mods,
pub mods: HashMap<String, String>,
}
#[derive(Deserialize, Serialize)]
@ -12,6 +14,3 @@ pub struct Server {
pub loader: String,
pub loader_version: Option<String>,
}
#[derive(Deserialize, Serialize)]
pub struct Mods {}