refactor(cli): centralize managed target preparation
Both client applets duplicated the step from parsed arguments to a runnable managed setup: target specs, managed target preparation, a ClientConfig built from an arbitrary primary target, pacing conversion, and an identical ManagedClientConfig skeleton with its own copy of the event-capacity constant. The primary-target step was the worst of it, since the managed driver replaces server_addr per target, so the address the applets went out of their way to find was immediately discarded. shared::client::prepare now performs that step once: it validates the target set first, then returns the validated targets, one target-free ClientConfig template, and the pacing, with managed_config()/managed_targets() for the driver. CommonClientArgs::to_client_config no longer takes a server address. Applet-specific semantics are unchanged: each command keeps its own duration default and help text, the client still accepts no target for --list-columns, and the TUI still rejects an empty target list at parse time.
This commit is contained in:
1 parent
c2666d776e
commit
50e4633c7a
8 files changed
+347
-128
No files matched your search
@@ -1,12 +1,10 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use clap::{Parser, ValueEnum};
|
||||
use irtt_client::ClientConfig;
|
||||
|
||||
use crate::shared::client::{
|
||||
parse_labelled_target, parse_test_duration, prepare_managed_targets, target_specs,
|
||||
CommonClientArgs, GroupPacingArg, LabelledTargetArg, PreparedTarget, TargetSpec, TimestampArg,
|
||||
parse_labelled_target, parse_test_duration, prepare_managed_run, CommonClientArgs,
|
||||
GroupPacingArg, LabelledTargetArg, ManagedRunSetup, TargetSelection, TimestampArg,
|
||||
};
|
||||
use clap::{Parser, ValueEnum};
|
||||
|
||||
pub const DEFAULT_CLIENT_DURATION: Duration = Duration::from_secs(10);
|
||||
|
||||
@@ -91,11 +89,19 @@ pub struct ClientArgs {
|
||||
}
|
||||
|
||||
impl ClientArgs {
|
||||
pub fn to_client_config(&self) -> ClientConfig {
|
||||
self.common.to_client_config(
|
||||
self.primary_target_addr()
|
||||
.expect("target is required unless --list-columns is set"),
|
||||
/// Validate the selected targets and prepare the managed run.
|
||||
///
|
||||
/// Targets are only required here. `--list-columns` returns before this is
|
||||
/// called, so listing columns still needs no target.
|
||||
pub fn prepare(&self) -> Result<ManagedRunSetup, String> {
|
||||
prepare_managed_run(
|
||||
&self.common,
|
||||
self.duration,
|
||||
TargetSelection {
|
||||
positional: &self.targets,
|
||||
labelled: &self.labelled_targets,
|
||||
pacing: self.pacing,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -106,22 +112,6 @@ impl ClientArgs {
|
||||
pub fn timestamp_mode(&self) -> TimestampArg {
|
||||
self.common.tstamp
|
||||
}
|
||||
|
||||
pub fn target_specs(&self) -> Result<Vec<CliTargetSpec>, String> {
|
||||
target_specs(&self.targets, &self.labelled_targets)
|
||||
}
|
||||
|
||||
pub fn managed_targets(&self) -> Result<Vec<CliManagedTarget>, String> {
|
||||
prepare_managed_targets(self.target_specs()?)
|
||||
}
|
||||
|
||||
fn primary_target_addr(&self) -> Option<&str> {
|
||||
self.targets.first().map(String::as_str).or_else(|| {
|
||||
self.labelled_targets
|
||||
.first()
|
||||
.map(|target| target.addr.as_str())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for ClientArgs {
|
||||
@@ -144,9 +134,6 @@ pub enum OutputFormat {
|
||||
Jsonl,
|
||||
}
|
||||
|
||||
pub type CliTargetSpec = TargetSpec;
|
||||
pub type CliManagedTarget = PreparedTarget;
|
||||
|
||||
impl OutputFormat {
|
||||
pub fn prints_summary(self) -> bool {
|
||||
matches!(self, Self::Table)
|
||||
@@ -191,7 +178,7 @@ mod tests {
|
||||
assert!(!args.is_continuous());
|
||||
assert_eq!(args.timestamp_mode(), TimestampArg::Both);
|
||||
|
||||
let config = args.to_client_config();
|
||||
let config = args.prepare().unwrap().client;
|
||||
assert_eq!(config.duration, Some(DEFAULT_CLIENT_DURATION));
|
||||
assert_eq!(config.received_stats, ReceivedStats::Both);
|
||||
assert_eq!(config.stamp_at, StampAt::Both);
|
||||
@@ -212,17 +199,22 @@ mod tests {
|
||||
let args = parse(&["--list-columns"]).unwrap();
|
||||
assert!(args.targets.is_empty());
|
||||
assert!(args.list_columns);
|
||||
// The parser accepts no target; preparation is where a target becomes
|
||||
// mandatory, and the stream command returns before preparing when
|
||||
// listing columns.
|
||||
let err = args.prepare().unwrap_err();
|
||||
assert!(err.contains("--list-columns"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_positional_targets_parse_as_targets() {
|
||||
let args = parse(&["host-a:2112", "host-b:2112"]).unwrap();
|
||||
let specs = args.target_specs().unwrap();
|
||||
let specs = args.prepare().unwrap().targets;
|
||||
|
||||
assert_eq!(specs[0].label, "host-a:2112");
|
||||
assert_eq!(specs[0].addr, "host-a:2112");
|
||||
assert_eq!(specs[0].managed.server_addr, "host-a:2112");
|
||||
assert_eq!(specs[1].label, "host-b:2112");
|
||||
assert_eq!(specs[1].addr, "host-b:2112");
|
||||
assert_eq!(specs[1].managed.server_addr, "host-b:2112");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -234,18 +226,18 @@ mod tests {
|
||||
"sg=sg.example.com:2112",
|
||||
])
|
||||
.unwrap();
|
||||
let specs = args.target_specs().unwrap();
|
||||
let specs = args.prepare().unwrap().targets;
|
||||
|
||||
assert_eq!(specs[0].label, "ams");
|
||||
assert_eq!(specs[0].addr, "ams.example.com:2112");
|
||||
assert_eq!(specs[0].managed.server_addr, "ams.example.com:2112");
|
||||
assert_eq!(specs[1].label, "sg");
|
||||
assert_eq!(specs[1].addr, "sg.example.com:2112");
|
||||
assert_eq!(specs[1].managed.server_addr, "sg.example.com:2112");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_labels_are_rejected() {
|
||||
let args = parse(&["host-a:2112", "--target", "host-a:2112=host-b:2112"]).unwrap();
|
||||
let err = args.target_specs().unwrap_err();
|
||||
let err = args.prepare().unwrap_err();
|
||||
|
||||
assert!(err.contains("duplicate target label"));
|
||||
}
|
||||
@@ -253,7 +245,7 @@ mod tests {
|
||||
#[test]
|
||||
fn duplicate_positional_target_strings_get_stable_suffixes() {
|
||||
let args = parse(&["host-a:2112", "host-a:2112"]).unwrap();
|
||||
let specs = args.target_specs().unwrap();
|
||||
let specs = args.prepare().unwrap().targets;
|
||||
|
||||
assert_eq!(specs[0].label, "host-a:2112");
|
||||
assert_eq!(specs[1].label, "host-a:2112#2");
|
||||
@@ -262,7 +254,7 @@ mod tests {
|
||||
#[test]
|
||||
fn duplicate_target_endpoints_are_allowed() {
|
||||
let args = parse(&["127.0.0.1:2112", "127.0.0.1"]).unwrap();
|
||||
assert_eq!(args.managed_targets().unwrap().len(), 2);
|
||||
assert_eq!(args.prepare().unwrap().target_count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user