refactor(cli): split main.rs into modules
server/ (state, ingestion, API, CSV exports, embedded assets), scanner, feeds_cmd, config_cmd and commands. Code moved as is; no behavior change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b70f36da09
commit
d231033e42
9 files changed
+1797
-1689
No files matched your search
@@ -0,0 +1,102 @@
|
||||
//! `xexeu config check`: validation report, effective schedules and overlapping groups.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::net::IpAddr;
|
||||
use std::path::Path;
|
||||
use std::process::ExitCode;
|
||||
|
||||
use crate::{fail, validated_config};
|
||||
use chrono::Utc;
|
||||
use xexeu_core::config::XexeuConfig;
|
||||
use xexeu_core::targets::{effective_cadence, expand_group, IpRange};
|
||||
|
||||
pub(crate) fn run_config_check(path: &Path) -> ExitCode {
|
||||
let (config, report) = match validated_config(path) {
|
||||
Ok(loaded) => loaded,
|
||||
Err(message) => return fail(&message),
|
||||
};
|
||||
|
||||
println!("Configuração válida: {}", path.display());
|
||||
for group in &report.groups {
|
||||
println!(
|
||||
"grupo {}: {} endereço(s), {} nome(s), perfil {}, cron {}",
|
||||
group.group, group.addresses, group.names, group.profile, group.cron
|
||||
);
|
||||
}
|
||||
|
||||
for warning in &report.warnings {
|
||||
println!("aviso: grupo {}: {}", warning.group, warning.message);
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
match overlapping_ranges(&config) {
|
||||
Ok(overlaps) if overlaps.is_empty() => {}
|
||||
Ok(overlaps) => {
|
||||
println!("\nSobreposições:");
|
||||
for range in overlaps {
|
||||
match effective_cadence(&config, range.start(), now) {
|
||||
Ok(Some(cadence)) => println!(
|
||||
" {} ({} endereço(s)): grupos [{}], efetivo {} a cada {}s, próxima {}",
|
||||
range,
|
||||
range.count(),
|
||||
cadence.groups.join(", "),
|
||||
cadence.effective_group,
|
||||
cadence.interval_secs,
|
||||
cadence.next_run.to_rfc3339()
|
||||
),
|
||||
Ok(None) => println!(
|
||||
" {} ({} endereço(s)): nenhum grupo cobre a sobreposição",
|
||||
range,
|
||||
range.count()
|
||||
),
|
||||
Err(error) => {
|
||||
println!(" {}: falha ao calcular a cadência efetiva: {error}", range)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(message) => return fail(&message),
|
||||
}
|
||||
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
fn overlapping_ranges(config: &XexeuConfig) -> Result<Vec<IpRange>, String> {
|
||||
let expansions = config
|
||||
.groups
|
||||
.iter()
|
||||
.map(expand_group)
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|error| format!("falha ao expandir os grupos: {error}"))?;
|
||||
|
||||
let mut seen = HashSet::new();
|
||||
let mut overlaps = Vec::new();
|
||||
for (index, left_group) in expansions.iter().enumerate() {
|
||||
for right_group in expansions.iter().skip(index + 1) {
|
||||
for left in left_group.ranges() {
|
||||
for right in right_group.ranges() {
|
||||
let start = left.start().max(right.start());
|
||||
let end = left.end().min(right.end());
|
||||
if start > end {
|
||||
continue;
|
||||
}
|
||||
if let Ok(range) = IpRange::new(start, end) {
|
||||
if seen.insert(range.to_string()) {
|
||||
overlaps.push(range);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
overlaps.sort_by_key(|range| (range.start().is_ipv6(), ip_value(range.start())));
|
||||
Ok(overlaps)
|
||||
}
|
||||
|
||||
fn ip_value(address: IpAddr) -> u128 {
|
||||
match address {
|
||||
IpAddr::V4(ip) => u32::from(ip) as u128,
|
||||
IpAddr::V6(ip) => u128::from(ip),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user