From 239cb91ad77b391a3b5581783ed2e474a7e1b420 Mon Sep 17 00:00:00 2001 From: bauen1 Date: Sat, 9 May 2020 21:47:08 +0200 Subject: [PATCH] add output file option --- src/main.rs | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 717c3c1..d212491 100644 --- a/src/main.rs +++ b/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 { 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 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 { 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, filters: Vec) -> Vec { } } - 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) -> 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, } 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 = 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(); }