Skip to content

Commit

Permalink
Don't mention ID in add error when only adding one mod, pull libium c…
Browse files Browse the repository at this point in the history
…hanges, and better filter CLI
  • Loading branch information
theRookieCoder committed Oct 19, 2024
1 parent d9a1017 commit 5691c95
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 195 deletions.
252 changes: 106 additions & 146 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,17 @@ tokio = { version = "1.40", default-features = false, features = [
"rt-multi-thread",
"macros",
] }
rustls = { version = "0.23", default-features = false, features = ["ring"] }
clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
serde_json = "1.0"
indicatif = "0.17"
octocrab = "0.40"
octocrab = "0.41"
fs_extra = "1.3"
ferinth = "2.11"
colored = "2.1"
futures = "0.3"
inquire = "0.7"
libium = { git = "https://github.com/gorilla-devs/libium", rev = "01d3aebe6c21e93376143b49d4bbd3e0325cb411" }
libium = { git = "https://github.com/gorilla-devs/libium", rev = "b7d2cd767b840313bc71dca643e4252c4e16dd3e" }
# libium = { path = "../libium" }
# libium = "1.32"
anyhow = "1.0"
Expand Down
11 changes: 11 additions & 0 deletions src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ pub fn display_successes_failures(successes: &[String], failures: Vec<(String, E
"Successfully added".green(),
successes.iter().map(|s| s.bold()).display(", ")
);

// No need to print the ID again if there is only one
} else if failures.len() == 1 {
let err = &failures[0].1;
return if matches!(err, libium::add::Error::AlreadyAdded) {
println!("{}", err.to_string().yellow());
false
} else {
println!("{}", err.to_string().red());
true
};
}

let mut grouped_errors = HashMap::new();
Expand Down
85 changes: 68 additions & 17 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![deny(missing_docs)]

use clap::{Parser, Subcommand, ValueEnum, ValueHint};
use clap::{Args, Parser, Subcommand, ValueEnum, ValueHint};
use clap_complete::Shell;
use libium::config::{filters::ReleaseChannel, structs::ModLoader};
use libium::config::{
filters::{self, Filter},
structs::ModLoader,
};
use std::path::PathBuf;

#[derive(Parser)]
Expand Down Expand Up @@ -55,21 +58,8 @@ pub enum SubCommands {
#[clap(long, short, visible_alias = "override")]
force: bool,

#[clap(long, short = 'l', group = "loader")]
mod_loader_prefer: Vec<ModLoader>,
#[clap(long, group = "loader")]
mod_loader_any: Vec<ModLoader>,

#[clap(long, short = 'v', group = "version")]
game_version_strict: Vec<String>,
#[clap(long, group = "version")]
game_version_minor: Vec<String>,

#[clap(long, short = 'c')]
release_channel: Option<ReleaseChannel>,

#[clap(long, short = 'n')]
file_name: Option<String>,
#[command(flatten)]
filters: FilterArguments,
},
/// Scan the profile's output directory (or the specified directory) for mods and add them to the profile
Scan {
Expand Down Expand Up @@ -258,6 +248,67 @@ pub enum ModpackSubCommands {
Upgrade,
}

#[derive(Args)]
#[group(id = "loader", multiple = false)]
// #[group(id = "version", multiple = false)]
pub struct FilterArguments {
#[clap(long, short = 'p')]
pub override_profile: bool,

#[clap(long, short = 'l', group = "loader")]
pub mod_loader_prefer: Vec<ModLoader>,
#[clap(long, group = "loader")]
pub mod_loader_any: Vec<ModLoader>,

#[clap(long, short = 'v', group = "version")]
pub game_version_strict: Vec<String>,
#[clap(long, group = "version")]
pub game_version_minor: Vec<String>,

#[clap(long, short = 'c')]
pub release_channel: Option<filters::ReleaseChannel>,

#[clap(long, short = 'n')]
pub filename: Option<String>,
#[clap(long, short = 't')]
pub title: Option<String>,
#[clap(long, short = 'd')]
pub description: Option<String>,
}

impl From<FilterArguments> for Vec<Filter> {
fn from(value: FilterArguments) -> Self {
let mut filters = vec![];

if !value.mod_loader_prefer.is_empty() {
filters.push(Filter::ModLoaderPrefer(value.mod_loader_prefer));
}
if !value.mod_loader_any.is_empty() {
filters.push(Filter::ModLoaderAny(value.mod_loader_any));
}
if !value.game_version_strict.is_empty() {
filters.push(Filter::GameVersionStrict(value.game_version_strict));
}
if !value.game_version_minor.is_empty() {
filters.push(Filter::GameVersionMinor(value.game_version_minor));
}
if let Some(release_channel) = value.release_channel {
filters.push(Filter::ReleaseChannel(release_channel));
}
if let Some(regex) = value.filename {
filters.push(Filter::Filename(regex));
}
if let Some(regex) = value.title {
filters.push(Filter::Title(regex));
}
if let Some(regex) = value.description {
filters.push(Filter::Description(regex));
}

filters
}
}

#[derive(Clone, Copy, Default, ValueEnum)]
pub enum Platform {
#[default]
Expand Down
10 changes: 5 additions & 5 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use fs_extra::{
};
use futures::{stream::FuturesUnordered, StreamExt as _};
use indicatif::ProgressBar;
use libium::{iter_ext::IterExt as _, upgrade::DownloadFile};
use libium::{iter_ext::IterExt as _, upgrade::DownloadData};
use std::{
ffi::OsString,
fs::{copy, create_dir_all, read_dir, remove_file},
Expand All @@ -26,10 +26,10 @@ use tokio::sync::Semaphore;
/// - If the file is a `.part` file or if the move failed, the file will be deleted
pub async fn clean(
directory: &Path,
to_download: &mut Vec<DownloadFile>,
to_download: &mut Vec<DownloadData>,
to_install: &mut Vec<(OsString, PathBuf)>,
) -> Result<()> {
let dupes = find_dupes_by_key(to_download, DownloadFile::filename);
let dupes = find_dupes_by_key(to_download, DownloadData::filename);
if !dupes.is_empty() {
println!(
"{}",
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn read_overrides(directory: &Path) -> Result<Vec<(OsString, PathBuf)>> {
/// Download and install the files in `to_download` and `to_install` to `output_dir`
pub async fn download(
output_dir: PathBuf,
to_download: Vec<DownloadFile>,
to_download: Vec<DownloadData>,
to_install: Vec<(OsString, PathBuf)>,
) -> Result<()> {
let progress_bar = Arc::new(Mutex::new(
Expand Down Expand Up @@ -128,7 +128,7 @@ pub async fn download(
let _permit = semaphore.acquire_owned().await?;

let (length, filename) = downloadable
.download(&client, &output_dir, |additional| {
.download(client, &output_dir, |additional| {
progress_bar
.lock()
.expect("Mutex poisoned")
Expand Down
25 changes: 8 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ fn main() -> ExitCode {

let cli = Ferium::parse();

let _ = rustls::crypto::ring::default_provider().install_default();

let mut builder = tokio::runtime::Builder::new_multi_thread();
builder.enable_all();
builder.thread_name("ferium-worker");
Expand Down Expand Up @@ -201,31 +199,22 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
}
}

let (successes, failures) = libium::add(profile, send_ids, !force).await?;
let (successes, failures) =
libium::add(profile, send_ids, !force, false, vec![]).await?;
spinner.finish_and_clear();

did_add_fail = add::display_successes_failures(&successes, failures);
}
SubCommands::Add {
identifiers,
force,
mod_loader_prefer,
mod_loader_any,
game_version_strict,
game_version_minor,
release_channel,
file_name,
filters,
} => {
let profile = get_active_profile(&mut config)?;
let override_profile = filters.override_profile;
let filters: Vec<_> = filters.into();

if identifiers.len() > 1
&& (!mod_loader_prefer.is_empty()
|| !mod_loader_any.is_empty()
|| !game_version_strict.is_empty()
|| !game_version_minor.is_empty()
|| release_channel.is_some()
|| file_name.is_some())
{
if identifiers.len() > 1 && !filters.is_empty() {
bail!("Only configure filters when adding a single mod!")
}

Expand All @@ -236,6 +225,8 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
.map(libium::add::parse_id)
.collect_vec(),
!force,
override_profile,
filters,
)
.await?;

Expand Down
10 changes: 5 additions & 5 deletions src/subcommands/modpack/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use libium::{
curseforge::structs::Manifest as CFManifest, modrinth::structs::Metadata as MRMetadata,
read_file_from_zip, zip_extract,
},
upgrade::{DistributionDeniedError, DownloadFile},
upgrade::{from_modpack_file, try_from_cf_file, DistributionDeniedError, DownloadData},
CURSEFORGE_API, HOME,
};
use std::{
Expand All @@ -24,7 +24,7 @@ use std::{
};

pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
let mut to_download: Vec<DownloadFile> = Vec::new();
let mut to_download: Vec<DownloadData> = Vec::new();
let mut to_install = Vec::new();
let install_msg;

Expand Down Expand Up @@ -61,8 +61,8 @@ pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
let mut tasks = FuturesUnordered::new();
let mut msg_shown = false;
for file in files {
match TryInto::<DownloadFile>::try_into(file) {
Ok(mut downloadable) => {
match try_from_cf_file(file) {
Ok((_, mut downloadable)) => {
downloadable.output = PathBuf::from(
if Path::new(&downloadable.filename())
.extension()
Expand Down Expand Up @@ -129,7 +129,7 @@ pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
)?;

for file in metadata.files {
to_download.push(file.into());
to_download.push(from_modpack_file(file));
}

install_msg = format!(
Expand Down
4 changes: 2 additions & 2 deletions src/subcommands/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use libium::{
filters::ProfileParameters as _,
structs::{ModLoader, Profile},
},
upgrade::{mod_downloadable, DownloadFile},
upgrade::{mod_downloadable, DownloadData},
};
use std::{
fs::read_dir,
Expand All @@ -26,7 +26,7 @@ use tokio::sync::Semaphore;
///
/// If an error occurs with a resolving task, instead of failing immediately,
/// resolution will continue and the error return flag is set to true.
pub async fn get_platform_downloadables(profile: &Profile) -> Result<(Vec<DownloadFile>, bool)> {
pub async fn get_platform_downloadables(profile: &Profile) -> Result<(Vec<DownloadData>, bool)> {
let to_download = Arc::new(Mutex::new(Vec::new()));
let progress_bar = Arc::new(Mutex::new(
ProgressBar::new(profile.mods.len() as u64).with_style(STYLE_NO.clone()),
Expand Down

0 comments on commit 5691c95

Please sign in to comment.