mirror of
https://gitlab.com/bauen1/dn42-roagen
synced 2024-05-11 05:55:36 +00:00
add output file option
This commit is contained in:
53
src/main.rs
53
src/main.rs
@ -1,6 +1,5 @@
|
||||
use std::fs::{ self, File};
|
||||
use std::io::BufRead;
|
||||
use structopt::StructOpt;
|
||||
use std::fs::{ self, File };
|
||||
use std::io::{ self, BufRead, Write};
|
||||
use cidr::{ IpCidr, Cidr};
|
||||
use std::convert::TryFrom;
|
||||
use std::str::FromStr;
|
||||
@ -8,6 +7,7 @@ use std::path::PathBuf;
|
||||
use std::fmt;
|
||||
use std::process::Command;
|
||||
use chrono::prelude::*;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug,PartialEq)]
|
||||
enum ROAFilterAction {
|
||||
@ -62,7 +62,6 @@ fn parse_filter(file: File) -> Vec<ROAFilter> {
|
||||
let fields: Vec<&str> = real_line.split_whitespace().collect();
|
||||
|
||||
if fields.len() >= 5 {
|
||||
//println!("fields: {:#?}", fields);
|
||||
results.push(ROAFilter::try_from(fields).expect("failed to parse filter"));
|
||||
}
|
||||
}
|
||||
@ -105,7 +104,6 @@ impl TryFrom<File> for ROA {
|
||||
|
||||
for line in std::io::BufReader::new(file).lines() {
|
||||
let real_line = line.unwrap();
|
||||
// println!("real_line: '{}'", real_line);
|
||||
let fields: Vec<&str> = real_line.split_whitespace().collect();
|
||||
|
||||
if (fields[0] == "route:") || (fields[0] == "route6:") {
|
||||
@ -143,7 +141,7 @@ fn parse_roa(path: PathBuf) -> Vec<ROA> {
|
||||
|
||||
match ROA::try_from(f) {
|
||||
Ok(roa) => results.push(roa),
|
||||
Err(e) => println!("err: {}", e),
|
||||
Err(e) => eprintln!("error while parsing roa: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
@ -159,20 +157,20 @@ fn roa_filter(roas: Vec<ROA>, filters: Vec<ROAFilter>) -> Vec<ROA> {
|
||||
}
|
||||
}
|
||||
|
||||
println!("# warning: dropped {:?}", roa);
|
||||
eprintln!("# warning: dropped {:?}", roa);
|
||||
return None;
|
||||
}).filter(|(roa, filter)|
|
||||
/* drop roa if the filter isn't permit */
|
||||
match filter.action {
|
||||
ROAFilterAction::Permit => true,
|
||||
ROAFilterAction::Deny => {
|
||||
println!("# warning: dropped {:?}", roa);
|
||||
eprintln!("# warning: dropped {:?}", roa);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
).filter_map(|(roa, filter)| {
|
||||
if roa.route.network_length() > filter.maxlen {
|
||||
println!("# warning: dropped {:?}", roa);
|
||||
eprintln!("# warning: dropped {:?}", roa);
|
||||
return None;
|
||||
}
|
||||
|
||||
@ -206,7 +204,10 @@ fn roa_to_output(roas: Vec<ROA>) -> String {
|
||||
#[derive(Debug)]
|
||||
struct Cli {
|
||||
#[structopt(parse(from_os_str))]
|
||||
registry_path: std::path::PathBuf,
|
||||
registry_path: PathBuf,
|
||||
|
||||
#[structopt(parse(from_os_str))]
|
||||
output: Option<PathBuf>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -220,12 +221,30 @@ fn main() {
|
||||
let roa_ip4 = parse_roa(data_path.join("route"));
|
||||
let roa_ip6 = parse_roa(data_path.join("route6"));
|
||||
|
||||
println!("#");
|
||||
println!("# dn42-roagen: simple dn42 roa generator");
|
||||
println!("# generated on: {}", Utc::now());
|
||||
print!("# commit: {}", String::from_utf8_lossy(&Command::new("git").arg("-C").arg(args.registry_path).arg("log").arg("-1").arg("--format=%H").output().expect("failed to determine git commit").stdout));
|
||||
println!("#");
|
||||
let date = Utc::now();
|
||||
let git_commit_output = Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(args.registry_path)
|
||||
.arg("-1")
|
||||
.arg("--format=%H")
|
||||
.output().expect("failed to determinte git commit")
|
||||
.stdout;
|
||||
|
||||
println!("{}", roa_to_output(roa_filter(roa_ip4, roa_filter_ip4)));
|
||||
println!("{}", roa_to_output(roa_filter(roa_ip6, roa_filter_ip6)));
|
||||
let git_commit = String::from_utf8_lossy(&git_commit_output);
|
||||
|
||||
let roa_output_ip4 = roa_to_output(roa_filter(roa_ip4, roa_filter_ip4));
|
||||
let roa_output_ip6 = roa_to_output(roa_filter(roa_ip6, roa_filter_ip6));
|
||||
|
||||
let mut output: Box<dyn Write> = match args.output {
|
||||
Some(path) => Box::new(File::create(path).expect("could not open output file")),
|
||||
None => Box::new(io::stdout()),
|
||||
};
|
||||
|
||||
writeln!(output, "#").unwrap();
|
||||
writeln!(output, "# dn42-roagen: simple dn42 roa generator").unwrap();
|
||||
writeln!(output, "# generated on: {}", date).unwrap();
|
||||
write!(output, "# commit: {}", git_commit).unwrap();
|
||||
writeln!(output, "#").unwrap();
|
||||
writeln!(output, "{}", roa_output_ip4).unwrap();
|
||||
writeln!(output, "{}", roa_output_ip6).unwrap();
|
||||
}
|
||||
|
Reference in New Issue
Block a user