CLI ergonomy: timeout value

This commit is contained in:
Jörn-Michael Miehe 2025-05-28 13:42:31 +00:00
parent 763c8f689e
commit 5643ced9d1
2 changed files with 17 additions and 8 deletions

View file

@ -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<Duration>,
/// 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<Duration, String> {
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, String> {
File::new(data).map_err(|e| e.to_string())
}
impl Cli {
pub fn get_timeout(&self) -> Option<Duration> {
(!self.timeout.is_zero()).then_some(self.timeout)
}
}

View file

@ -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();