feat: add slate file delition, remove unnecessary feature
This commit is contained in:
61
src/lib.rs
61
src/lib.rs
@ -15,24 +15,11 @@ pub async fn run(args: Args) -> Result<()> {
|
||||
println!(" Output directory: {}", args.output.display());
|
||||
println!(" Concurrency level: {}", args.concurrency);
|
||||
|
||||
// Validate that --domain and --rule-path are used together.
|
||||
args.validate_domain_and_rule_path()?;
|
||||
|
||||
// Load and parse the configuration file into strongly-typed structs.
|
||||
let mut config = Config::load(&args.input)
|
||||
let config = Config::load(&args.input)
|
||||
.with_context(|| format!("Failed to load config from {}", args.input.display()))?;
|
||||
|
||||
let urls_to_download = config.extract_urls();
|
||||
if urls_to_download.is_empty() {
|
||||
println!("✔️ No rule sets with URLs found in the configuration. Nothing to do.");
|
||||
return Ok(());
|
||||
}
|
||||
println!(
|
||||
"✔️ Found {} rule sets to download.",
|
||||
urls_to_download.len()
|
||||
);
|
||||
|
||||
// Ensure the output directory exists.
|
||||
// Ensure the output directory exists before any operations.
|
||||
fs::create_dir_all(&args.output).with_context(|| {
|
||||
format!(
|
||||
"Failed to create output directory '{}'",
|
||||
@ -40,6 +27,25 @@ pub async fn run(args: Args) -> Result<()> {
|
||||
)
|
||||
})?;
|
||||
|
||||
// Determine the set of files that should exist based on the config.
|
||||
let expected_files = config
|
||||
.get_expected_files(&args.output)
|
||||
.context("Failed to determine expected files from config")?;
|
||||
|
||||
// Clean up any files in the output directory that are not in our expected set.
|
||||
downloader::cleanup_stale_files(&args.output, &expected_files)?;
|
||||
|
||||
// Proceed to download files defined in the config.
|
||||
let urls_to_download = config.extract_urls();
|
||||
if urls_to_download.is_empty() {
|
||||
println!("\n✔️ No rule sets with URLs found. Process complete.");
|
||||
return Ok(());
|
||||
}
|
||||
println!(
|
||||
"\n✔️ Found {} rule sets to download/update.",
|
||||
urls_to_download.len()
|
||||
);
|
||||
|
||||
// Download all files concurrently.
|
||||
let download_report =
|
||||
downloader::download_all_rules(&urls_to_download, &args.output, args.concurrency).await?;
|
||||
@ -50,33 +56,14 @@ pub async fn run(args: Args) -> Result<()> {
|
||||
download_report.successful, download_report.failed
|
||||
);
|
||||
|
||||
// If any downloads failed, abort before modifying the config file.
|
||||
// If any downloads failed, abort with an error message.
|
||||
if download_report.failed > 0 {
|
||||
bail!(
|
||||
"Aborting due to {} download failures. The configuration file was NOT modified.",
|
||||
"Aborting due to {} download failures.",
|
||||
download_report.failed
|
||||
);
|
||||
}
|
||||
|
||||
// Rewrite and save the config file only if all downloads were successful
|
||||
// and the domain/rule_path arguments were provided.
|
||||
if let (Some(domain), Some(rule_path)) = (args.domain, args.rule_path) {
|
||||
println!("\n▶️ All downloads successful. Rewriting configuration file...");
|
||||
|
||||
config.rewrite_urls(&domain, &rule_path)?;
|
||||
config.save(&args.input).with_context(|| {
|
||||
format!("Failed to write updated config to {}", args.input.display())
|
||||
})?;
|
||||
|
||||
println!(
|
||||
"✔️ Configuration file {} updated successfully.",
|
||||
args.input.display()
|
||||
);
|
||||
} else {
|
||||
println!(
|
||||
"\n✔️ Downloads complete. No domain/rule-path specified; config file not modified."
|
||||
);
|
||||
}
|
||||
|
||||
println!("\n✔️ Ruleset synchronization complete.");
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user