From 2f4c6b73aaa5986e34464979c4d966229bb4a801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 28 May 2025 13:42:31 +0000 Subject: [PATCH] CLI ergonomy: timeout value --- src/cli.rs | 22 +++++++++++++++------- src/main.rs | 3 ++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index eb31672..3e45279 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,15 +7,19 @@ use super::sharry::File; #[derive(Parser, Debug, Hash)] #[command(version, about, long_about = None)] pub struct Cli { - /// Timeout in seconds for HTTP actions - #[arg(short, long, value_name = "SECS", value_parser = parse_seconds)] - pub timeout: Option, + /// Timeout in seconds for HTTP actions (set 0 or invalid to disable) + #[arg( + short, long, + default_value = "10", value_name = "SECS", + value_parser = parse_seconds, + )] + timeout: Duration, /// Protocol for Sharry instance #[arg( short, long, default_value = "https", value_name = "VARIANT", - value_parser = PossibleValuesParser::new(["http", "https"]) + value_parser = PossibleValuesParser::new(["http", "https"]), )] pub protocol: String, @@ -47,11 +51,15 @@ pub struct Cli { } fn parse_seconds(data: &str) -> Result { - data.parse() - .map(Duration::from_secs) - .map_err(|e| e.to_string()) + data.parse().or(Ok(0)).map(Duration::from_secs) } fn parse_sharry_file(data: &str) -> Result { File::new(data).map_err(|e| e.to_string()) } + +impl Cli { + pub fn get_timeout(&self) -> Option { + (!self.timeout.is_zero()).then(|| self.timeout) + } +} diff --git a/src/main.rs b/src/main.rs index 67d032c..2f51b84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,9 +13,10 @@ fn main() { let args = Cli::parse(); info!("args: {args:?}"); + info!("timeout: {:?}", args.get_timeout()); let agent: Agent = Agent::config_builder() - .timeout_global(args.timeout) + .timeout_global(args.get_timeout()) .build() .into();