Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MRT output file to parse command #64

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add MRT output file writing functionality
Introduce support for writing filtered MRT messages to a file. Added
a new `mrt_path` CLI argument and implemented handling to write MRT
output to the specified path when provided. Retained existing stdout
behavior as the default.
digizeph committed Dec 27, 2024
commit a4da9214bd888c8c13b879caaec71301c1e8e51f
45 changes: 38 additions & 7 deletions src/bin/monocle.rs
Original file line number Diff line number Diff line change
@@ -204,6 +204,10 @@ enum Commands {
#[clap(long)]
pretty: bool,

/// MRT output file path
#[clap(long, short = 'M')]
mrt_path: Option<PathBuf>,

/// Filter by AS path regex string
#[clap(flatten)]
filters: ParseFilters,
@@ -228,7 +232,7 @@ enum Commands {
sqlite_path: Option<PathBuf>,

/// MRT output file path
#[clap(long)]
#[clap(long, short = 'M')]
mrt_path: Option<PathBuf>,

/// SQLite reset database content if exists
@@ -421,6 +425,7 @@ fn main() {
file_path,
json,
pretty,
mrt_path,
filters,
} => {
if let Err(e) = filters.validate() {
@@ -445,13 +450,39 @@ fn main() {
.unwrap();

let mut stdout = std::io::stdout();
for elem in parser {
let output_str = elem_to_string(&elem, json, pretty, "");
if let Err(e) = writeln!(stdout, "{}", &output_str) {
if e.kind() != std::io::ErrorKind::BrokenPipe {
eprintln!("{e}");

match mrt_path {
None => {
for elem in parser {
// output to stdout
let output_str = elem_to_string(&elem, json, pretty, "");
if let Err(e) = writeln!(stdout, "{}", &output_str) {
if e.kind() != std::io::ErrorKind::BrokenPipe {
eprintln!("{e}");
}
std::process::exit(1);
}
}
}
Some(p) => {
let path = p.to_str().unwrap().to_string();
println!("processing. filtered messages output to {}...", &path);
let mut encoder = MrtUpdatesEncoder::new();
let mut writer = match oneio::get_writer(&path) {
Ok(w) => w,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
let mut total_count = 0;
for elem in parser {
total_count += 1;
encoder.process_elem(&elem);
}
std::process::exit(1);
writer.write_all(&encoder.export_bytes()).unwrap();
drop(writer);
println!("done. total of {} message wrote", total_count);
}
}
}