Skip to content

Commit

Permalink
Merge pull request #59 from Prevter/main
Browse files Browse the repository at this point in the history
add "Install mod" option for index admin
  • Loading branch information
Fleeym authored Jun 7, 2024
2 parents e380adb + cf94ce3 commit 26b37e6
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/index_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct PendingModVersion {
geode: String,
early_load: bool,
api: bool,
mod_id: String,
gd: PendingModGD,
dependencies: Option<Vec<PendingModDepencency>>,
incompatibilities: Option<Vec<PendingModDepencency>>,
Expand All @@ -69,6 +70,7 @@ impl Display for PendingModVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", self.version)?;
writeln!(f, " - Name: {}", self.name)?;
writeln!(f, " - ID: {}", self.mod_id)?;
writeln!(
f,
" - Description: {}",
Expand Down Expand Up @@ -207,6 +209,7 @@ fn list_pending_mods(config: &Config) {
println!(" - <INDEX>: Go to submission");
println!(" - v: Validate mod");
println!(" - r: Reject mod");
println!(" - i: Install mod");
println!(" - q: Quit");
println!("---------------------");

Expand Down Expand Up @@ -245,7 +248,21 @@ fn list_pending_mods(config: &Config) {
} else {
let version = ask_value("Version", None, true);
if let Some(version) = version_vec.iter().find(|x| x.version == version) {
validate_mod(version, &mods.data[0].id, config);
reject_mod(version, &mods.data[0].id, config);
} else {
warn!("Invalid version");
}
}
}
"i" => {
let version_vec: &Vec<PendingModVersion> = mods.data[0].versions.as_ref();

if version_vec.len() == 1 {
download_mod(&version_vec[0], &mods.data[0].id, config);
} else {
let version = ask_value("Version", None, true);
if let Some(version) = version_vec.iter().find(|x| x.version == version) {
download_mod(version, &mods.data[0].id, config);
} else {
warn!("Invalid version");
}
Expand Down Expand Up @@ -402,6 +419,34 @@ fn reject_mod(version: &PendingModVersion, id: &str, config: &Config) {
info!("Mod rejected");
}

fn download_mod(version: &PendingModVersion, id: &str, config: &Config) {
let client = reqwest::blocking::Client::new();
let path = format!("v1/mods/{}/versions/{}/download", id, version.version);
let url = index::get_index_url(path, config);

let response = client
.get(url)
.bearer_auth(config.index_token.clone().unwrap())
.send()
.nice_unwrap("Failed to connect to the Geode Index");

if response.status() != 200 {
if let Ok(body) = response.json::<ApiResponse<String>>() {
warn!("{}", body.error);
}
fatal!("Bad response from Geode Index");
}

let data = response.bytes().nice_unwrap("Failed to download mod");

let mods_dir = config.get_current_profile().mods_dir();
let mod_path = mods_dir.join(format!("{}.geode", version.mod_id));

std::fs::write(&mod_path, data).nice_unwrap("Failed to save mod");

info!("Mod downloaded");
}

pub fn get_random_message() -> String {
let messages = [
"[BUZZER]",
Expand Down

0 comments on commit 26b37e6

Please sign in to comment.