Files
rmcl/src/cli/import.rs
T
objz 1f1a0ebb3d refactored: test style cleanup + mmc archive extract test
Picks up the style audit's findings without touching production
behaviour (except for two em-dash characters in non-test code that
violated the no-em-dash rule).

- cli/import.rs, instance/import/mmc.rs: replace the two em-dashes
  that leaked into source (one in a user-facing format string, one
  in a code comment) with hyphen / colon.
- instance/manager.rs: rewrite test_manager to return TempDir instead
  of a hand-rolled tempdir path with manual cleanup. Drop the
  trailing std::fs::remove_dir_all from every test. Rename test_*
  functions to describe the scenario per CONTRIBUTING (delete_missing_
  instance_returns_not_found, save_then_load_all_round_trips_config,
  load_one_missing_returns_not_found, etc.).
- migrate.rs: replace unique_tmp + atomic counter with
  tempfile::tempdir() across all seven tests; ~10 lines of plumbing
  gone, automatic cleanup via Drop.
- auth/accounts.rs: drop the dummy_account wrapper that just forwarded
  to create_offline_account; inline the real call at the eight use
  sites.
- instance/models.rs: rename test_instance_config_roundtrip to
  instance_config_roundtrips_through_json.
- instance/content/mods.rs: drop the "what" comments above straight
  forward asserts; keep the one "why" comment that explains an
  empty-name + present-logo coverage gap. Trim the obvious "helper to
  create a jar (zip) file" comment above make_jar.
- tests/smoke.rs: //! to // for the file header; project convention is
  // for files (//! is for crate-level rustdoc, which doesn't render
  for integration test crates).

Adds one new test:

- instance/import/mmc.rs: extract_mmc_archive_copies_minecraft_subtree
  builds an in-memory zip with a Pack/ prefix, a Pack/mmc-pack.json
  sibling, and Pack/.minecraft/{options.txt, mods/test-mod.jar},
  then asserts only the .minecraft/ subtree lands in the destination
  with relative paths preserved. Closes the mmc extract coverage gap
  the audit flagged.
2026-05-27 17:09:09 +02:00

113 lines
4.1 KiB
Rust

// modpack importing from modrinth (remote slug/url) or local files (.mrpack, mmc zips).
// resolves the input, downloads if needed, then hands off to the import engine.
use clap::ArgMatches;
use crate::instance::InstanceManager;
use crate::instance::import::{ImportInput, parse_import_input};
use crate::net::modrinth;
type CliResult = Result<(), Box<dyn std::error::Error>>;
pub async fn handle_import(matches: &ArgMatches) -> CliResult {
let input = matches.get_one::<String>("source").unwrap();
let override_name = matches.get_one::<String>("name");
let override_version = matches.get_one::<String>("version");
let instances_dir = crate::config::SETTINGS.paths.resolve_instances_dir();
let meta_dir = crate::config::SETTINGS.paths.resolve_meta_dir();
let manager = InstanceManager::new(instances_dir, meta_dir);
let client = crate::net::HttpClient::new();
let parsed = parse_import_input(input);
let archive_path = match parsed {
ImportInput::LocalFile(ref path) => {
let resolved = crate::config::settings::resolve_path(path);
if !resolved.exists() {
return Err(format!("File not found: {}", resolved.display()).into());
}
resolved
}
ImportInput::ProjectSlug(ref slug) => {
println!("Fetching project '{slug}'...");
let project = modrinth::fetch_project(&client, slug)
.await
.map_err(|e| format!("Failed to fetch project: {e}"))?;
println!("Found: {}", project.title);
let versions = modrinth::fetch_versions(&client, slug)
.await
.map_err(|e| format!("Failed to fetch versions: {e}"))?;
if versions.is_empty() {
return Err("No versions found for this modpack".into());
}
let version = if let Some(version_name) = override_version {
versions
.iter()
.find(|v| v.version_number == *version_name || v.name == *version_name)
.ok_or_else(|| format!("Version '{version_name}' not found"))?
} else {
&versions[0]
};
println!(
"Using version {} ({})",
version.version_number,
version.game_versions.first().unwrap_or(&"?".to_string())
);
let tmp_dir = manager.meta_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir)?;
modrinth::download_mrpack(&client, version, &tmp_dir)
.await
.map_err(|e| format!("Failed to download .mrpack: {e}"))?
}
ImportInput::VersionId {
slug: _,
ref version_id,
} => {
println!("Fetching version '{version_id}'...");
let version = modrinth::fetch_version(&client, version_id)
.await
.map_err(|e| format!("Failed to fetch version: {e}"))?;
println!(
"Found: {} ({})",
version.version_number,
version.game_versions.first().unwrap_or(&"?".to_string())
);
let tmp_dir = manager.meta_dir.join("tmp");
std::fs::create_dir_all(&tmp_dir)?;
modrinth::download_mrpack(&client, &version, &tmp_dir)
.await
.map_err(|e| format!("Failed to download .mrpack: {e}"))?
}
};
let mut summary = crate::instance::import::build_summary(&archive_path)
.map_err(|e| format!("Invalid modpack: {e}"))?;
if let Some(name) = override_name {
summary.name = name.clone();
}
println!(
"Importing '{}' - {} {} ({} mods, {} overrides)",
summary.name,
summary.game_version,
summary.loader,
summary.mod_count,
summary.override_count
);
let config = crate::instance::import::execute_import(&summary, &manager)
.await
.map_err(|e| format!("Import failed: {e}"))?;
println!("Instance '{}' created successfully.", config.name);
Ok(())
}