|
1 | 1 | use clap::Parser; |
2 | 2 | use regex::Regex; |
3 | 3 | use std::time::Duration; |
| 4 | +use thiserror::Error; |
4 | 5 |
|
5 | 6 | pub struct TlsConfig { |
6 | 7 | pub secret_name: Option<String>, |
@@ -183,6 +184,14 @@ pub struct CliArgs { |
183 | 184 | #[arg(long, env = "CMK_PULL_SHARED_SECRET", hide_env_values = true)] |
184 | 185 | pub pull_shared_secret: Option<String>, |
185 | 186 |
|
| 187 | + /// Push interval in seconds for push mode. Ignored if push mode is not enabled. |
| 188 | + #[arg( |
| 189 | + long = "push-interval", |
| 190 | + value_parser = parse_push_interval, |
| 191 | + default_value = "60" |
| 192 | + )] |
| 193 | + pub push_interval: Duration, |
| 194 | + |
186 | 195 | /// Enable push mode and send sections to the specified server (including |
187 | 196 | /// port) |
188 | 197 | #[arg(long = "push-receiver")] |
@@ -278,6 +287,23 @@ fn parse_duration_days(arg: &str) -> Result<Duration, std::num::ParseIntError> { |
278 | 287 | Ok(Duration::from_secs(60 * 60 * 24 * days)) |
279 | 288 | } |
280 | 289 |
|
| 290 | +#[derive(Error, Debug)] |
| 291 | +enum PushIntervalError { |
| 292 | + #[error("must be a whole number of seconds")] |
| 293 | + NotANumber(#[from] std::num::ParseIntError), |
| 294 | + #[error("must be greater than zero")] |
| 295 | + Zero, |
| 296 | +} |
| 297 | + |
| 298 | +fn parse_push_interval(arg: &str) -> Result<Duration, PushIntervalError> { |
| 299 | + let interval = parse_duration_secs(arg)?; |
| 300 | + // if 0, tokio panics |
| 301 | + match interval.is_zero() { |
| 302 | + true => Err(PushIntervalError::Zero), |
| 303 | + false => Ok(interval), |
| 304 | + } |
| 305 | +} |
| 306 | + |
281 | 307 | #[cfg(test)] |
282 | 308 | mod tests { |
283 | 309 | use super::*; |
@@ -371,4 +397,21 @@ mod tests { |
371 | 397 | ); |
372 | 398 | } |
373 | 399 | } |
| 400 | + |
| 401 | + #[test] |
| 402 | + fn push_interval_parse_correctly() { |
| 403 | + for interval in ["1", "80"] { |
| 404 | + assert!(parse(&["--push-interval", interval]).is_ok()); |
| 405 | + } |
| 406 | + } |
| 407 | + |
| 408 | + #[test] |
| 409 | + fn push_interval_cannot_be_zero() { |
| 410 | + assert_eq!( |
| 411 | + parse(&["--push-interval", "0"]) |
| 412 | + .expect_err("push interval of zero should fail") |
| 413 | + .kind(), |
| 414 | + ErrorKind::ValueValidation |
| 415 | + ); |
| 416 | + } |
374 | 417 | } |
0 commit comments