CLI ergonomy: timeout value
This commit is contained in:
parent
763c8f689e
commit
2f4c6b73aa
2 changed files with 17 additions and 8 deletions
22
src/cli.rs
22
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<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(|| self.timeout)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue