Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scanning functionality #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ pub enum SubCommands {
},
/// Download and install the latest version of the mods specified
Upgrade,
#[clap(about("Scan profile for mods"))]
Scan {
#[clap(long)]
#[clap(arg_enum)]
#[clap(help("Preferred platform to scan mods from (default: modrinth)"))]
preferred_platform: Option<libium::config::structs::ModPlatform>
}
}

#[derive(Subcommand)]
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ async fn actual_main(cli_app: Ferium) -> Result<()> {
check_empty_profile(profile)?;
subcommands::upgrade(modrinth, curseforge, github, profile).await?;
},
SubCommands::Scan { preferred_platform } => {
check_internet().await?;
let profile = get_active_profile(&mut config)?;
subcommands::scan(
modrinth,
curseforge,
profile,
preferred_platform.unwrap_or(libium::config::structs::ModPlatform::Modrinth),
)
.await?;
},
};

config.profiles.iter_mut().for_each(|profile| {
Expand Down
2 changes: 2 additions & 0 deletions src/subcommands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ pub mod modpack;
pub mod profile;
mod remove;
mod upgrade;
mod scan;
pub use remove::remove;
pub use upgrade::upgrade;
pub use scan::scan;
127 changes: 127 additions & 0 deletions src/subcommands/scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use std::{ffi::OsStr, fs, sync::Arc};

use anyhow::{bail, Result};
use colored::Colorize;
use ferinth::Ferinth;
use furse::Furse;
use libium::{
add,
config::structs::{Mod, ModIdentifier, ModPlatform, Profile},
scan,
};

use crate::{CROSS, TICK, YELLOW_TICK};
pub async fn scan(
modrinth: Arc<Ferinth>,
curseforge: Arc<Furse>,
profile: &mut Profile,
preferred_platform: libium::config::structs::ModPlatform,
) -> Result<()> {
let mods = scan::scan(
modrinth.clone(),
curseforge.clone(),
fs::read_dir(&profile.output_dir)?
.filter_map(|path| {
if let Ok(entry) = path {
let file_path = entry.path();
if matches!(file_path.extension().and_then(OsStr::to_str), Some("jar")) {
Some(file_path)
} else {
None
}
} else {
None
}
})
.collect(),
)
.await?;
let mut mods_to_add: Vec<(std::path::PathBuf, ModIdentifier)> = vec![];
for (path, mod_) in mods {
if !matches!(mod_, (None, None)) {
let mod_to_add = match (&mod_, &preferred_platform) {
((Some(modrinth_mod), _), ModPlatform::Modrinth) => {
ModIdentifier::ModrinthProject(modrinth_mod.project_id.clone())
},
((_, Some(curseforge_mod)), ModPlatform::Curseforge) => {
ModIdentifier::CurseForgeProject(curseforge_mod.mod_id)
},
_ => match &mod_ {
(Some(modrinth_mod), _) => {
ModIdentifier::ModrinthProject(modrinth_mod.project_id.clone())
},
(_, Some(curseforge_mod)) => {
ModIdentifier::CurseForgeProject(curseforge_mod.mod_id)
},
_ => unreachable!(),
},
};
mods_to_add.push((path, mod_to_add));
} else {
eprintln!(
"{}",
format!(
"{} Could not find {} on any platform",
CROSS,
path.display()
)
.red()
);
}
}
for (path, mod_) in mods_to_add {
match add_mod(modrinth.clone(), curseforge.clone(), mod_, profile).await {
Ok(mod_) => {
println!(
"{} found {} on {}",
TICK.clone(),
&mod_.name,
match &mod_.identifier {
ModIdentifier::CurseForgeProject(_) => "CurseForge",
ModIdentifier::ModrinthProject(_) => "Modrinth",
_ => unreachable!(),
}
);
profile.mods.push(mod_);
},
Err(add::Error::AlreadyAdded) => {
println!(
"{} {} is already added",
YELLOW_TICK.clone(),
path.display()
)
},
Err(err) => bail!(err),
}
}
Ok(())
}

async fn add_mod(
modrinth: Arc<Ferinth>,
curseforge: Arc<Furse>,
mod_: ModIdentifier,
profile: &Profile,
) -> Result<Mod, add::Error> {
match &mod_ {
ModIdentifier::ModrinthProject(id) => {
let (project, _version) = add::modrinth(modrinth, id, profile, None, None).await?;
Ok(Mod {
check_game_version: None,
check_mod_loader: None,
identifier: mod_,
name: project.title,
})
},
ModIdentifier::CurseForgeProject(id) => {
let (project, _file) = add::curseforge(curseforge, *id, profile, None, None).await?;
Ok(Mod {
check_game_version: None,
check_mod_loader: None,
identifier: mod_,
name: project.name,
})
},
_ => unreachable!(),
}
}