Skip to content

Commit 4215c04

Browse files
committed
optionally read probes from a file with the client
1 parent c20254b commit 4215c04

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/client/handler.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
use anyhow::Result;
2+
use caracat::models::Probe;
23
use log::trace;
34
use std::io::{stdin, BufRead};
5+
use std::path::PathBuf;
46

57
use crate::auth::{KafkaAuth, SaslAuth};
68
use crate::client::producer::produce;
79
use crate::config::AppConfig;
810
use crate::probe::decode_probe;
911

10-
pub async fn handle(config: &AppConfig, agents: &str) -> Result<()> {
12+
fn read_probes<R: BufRead>(buf_reader: R) -> Result<Vec<Probe>> {
13+
let mut probes = Vec::new();
14+
for line in buf_reader.lines() {
15+
let probe = line?;
16+
probes.push(decode_probe(&probe)?);
17+
}
18+
Ok(probes)
19+
}
20+
21+
pub async fn handle(config: &AppConfig, agents: &str, probes_file: Option<PathBuf>) -> Result<()> {
1122
trace!("Client handler");
1223
trace!("{:?}", config);
1324

@@ -26,16 +37,24 @@ pub async fn handle(config: &AppConfig, agents: &str) -> Result<()> {
2637
}
2738
};
2839

29-
// Get probes from stdin
30-
let mut probes = Vec::new();
31-
for line in stdin().lock().lines() {
32-
let probe = line?;
33-
probes.push(decode_probe(&probe)?);
34-
}
40+
// Read probes from file or stdin
41+
let probes = match probes_file {
42+
Some(probes_file) => {
43+
let file = std::fs::File::open(probes_file)?;
44+
let buf_reader = std::io::BufReader::new(file);
45+
read_probes(buf_reader)?
46+
}
47+
None => {
48+
let stdin = stdin();
49+
let buf_reader = stdin.lock();
50+
read_probes(buf_reader)?
51+
}
52+
};
3553

36-
// Split agents
54+
// Split the agents
3755
let agents = agents.split(',').collect::<Vec<&str>>();
3856

57+
// Produce Kafka messages
3958
produce(config, auth, agents, probes).await;
4059

4160
Ok(())

src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use clap_verbosity_flag::{InfoLevel, Verbosity};
1212
use env_logger::Builder;
1313
use log::error;
1414
use std::io::{stdin, IsTerminal, Write};
15+
use std::path::PathBuf;
1516

1617
use crate::config::app_config;
1718

@@ -41,6 +42,10 @@ enum Command {
4142
/// Agent IDs (comma separated)
4243
#[arg(index = 1)]
4344
agents: String,
45+
46+
/// Probes file (read stdin if not provided)
47+
#[arg(short, long)]
48+
probes_file: Option<PathBuf>,
4449
},
4550
}
4651

@@ -79,14 +84,18 @@ async fn main() -> Result<()> {
7984
Err(e) => error!("Error: {}", e),
8085
}
8186
}
82-
Command::Client { config, agents } => {
87+
Command::Client {
88+
config,
89+
agents,
90+
probes_file,
91+
} => {
8392
if stdin().is_terminal() {
8493
App::command().print_help().unwrap();
8594
::std::process::exit(2);
8695
}
8796

8897
let app_config = app_config(&config);
89-
match client::handle(&app_config, &agents).await {
98+
match client::handle(&app_config, &agents, probes_file).await {
9099
Ok(_) => (),
91100
Err(e) => error!("Error: {}", e),
92101
}

0 commit comments

Comments
 (0)