Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions pkgs/fleet/crates/fleet-proxy/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ pub struct RequestArgs {
pub body: Option<serde_json::Value>,
}

/// The `fleet proxy connect` usage text, shared by the parser's
/// help arm and the bin's help intercept, which prints it to
/// stdout at exit 0 (#5619).
pub const CONNECT_USAGE: &str = "\
usage: fleet proxy connect [--print-path] [--stdio [--host H] \
[--port N]] [--request METHOD PATH [--service-port N] \
[--token-file F] [--body JSON]]

Connect to the session proxy: ensure a broker answers (starting
one when none does), sync the PATH shims from its services map,
and optionally issue one request over the negotiated stream.";

impl ConnectArgs {
pub fn parse(argv: &[String]) -> Result<ConnectArgs, ClientError> {
let mut out = ConnectArgs::default();
Expand Down Expand Up @@ -333,12 +345,7 @@ impl ConnectArgs {
.body = Some(body);
}
"--help" | "-h" => {
return Err(ClientError::Usage(
"usage: fleet proxy connect [--print-path] [--stdio [--host H] \
[--port N]] [--request METHOD PATH [--service-port N] \
[--token-file F] [--body JSON]]"
.to_string(),
));
return Err(ClientError::Usage(CONNECT_USAGE.to_string()));
}
other => return Err(usage(&format!("unknown argument {other:?}"))),
}
Expand Down Expand Up @@ -428,6 +435,16 @@ pub fn run_connect(
Ok(0)
}

/// The `fleet init` usage text (#5619).
pub const INIT_USAGE: &str = "\
Usage: fleet init

Autostart the session proxy if it is not running: probe the broker
socket, sync the PATH shims when a broker answers, and start one
when none does. A strict no-op when a broker is already running.

Takes no arguments.";

/// `fleet init` — autostart semantics (§8): probe first; a live
/// broker (either dialect) means status + shim sync and out,
/// never a spawn, never a restart. Only an unanswered socket
Expand All @@ -438,6 +455,13 @@ pub fn run_init(
client: &str,
capabilities: &[String],
) -> Result<i32, ClientError> {
// A help flag asks for the usage: print it and succeed, like
// the other fleet leaves (#5619) - it is not an illegal
// argument, and asking must never probe, spawn, or sync shims.
if argv.iter().any(|a| matches!(a.as_str(), "--help" | "-h")) {
println!("{INIT_USAGE}");
return Ok(0);
}
if !argv.is_empty() {
return Err(ClientError::Usage(format!(
"fleet init takes no arguments (got {:?})",
Expand Down Expand Up @@ -486,3 +510,33 @@ pub fn run_init(
);
Ok(0)
}

#[cfg(test)]
mod tests {
use super::*;

fn strings(list: &[&str]) -> Vec<String> {
list.iter().map(|s| s.to_string()).collect()
}

#[test]
fn init_help_succeeds_without_touching_the_broker() {
// #5619: --help/-h print the init usage and return Ok(0)
// before the takes-no-arguments rejection and before any
// socket probe, so these calls double as the
// no-side-effect pins.
let config = Config::default();
for flag in ["--help", "-h"] {
assert_eq!(
run_init(&strings(&[flag]), &config, "test-client", &[]).ok(),
Some(0),
"{flag} must print the init usage and succeed"
);
}
// A non-help argument is still a usage error (exit 2).
let err = run_init(&strings(&["--bogus"]), &config, "test-client", &[])
.expect_err("a non-help argument must be rejected");
assert_eq!(err.exit_code(), EXIT_USAGE);
assert!(err.to_string().contains("--bogus"));
}
}
32 changes: 32 additions & 0 deletions pkgs/fleet/crates/fleet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,18 @@ fn run_proxy(surface: &Surface, args: &[String]) -> i32 {
}
},
Some("connect") => {
// A help flag in the tail asks for the leaf's usage:
// print it to stdout and succeed, like the dispatch
// verbs and proxy serve (#5560/#5575). ConnectArgs::parse
// can only carry the text out as a usage error (exit 2),
// so the help form is answered here (#5619).
if args[1..]
.iter()
.any(|a| matches!(a.as_str(), "--help" | "-h"))
{
println!("{}", fleet_proxy::client::CONNECT_USAGE);
return EXIT_OK;
}
let parsed = match fleet_proxy::client::ConnectArgs::parse(&args[1..]) {
Ok(a) => a,
Err(e) => {
Expand Down Expand Up @@ -814,6 +826,26 @@ mod tests {
);
}

#[test]
fn proxy_connect_and_init_help_exit_ok() {
// #5619: connect/init --help answered through the error
// channel (exit 2, stderr). Both now print usage to stdout
// and succeed, before any broker probe or spawn.
let surface = Surface::new(None, fleet_config::Config::default());
for flag in ["--help", "-h"] {
assert_eq!(
run_proxy(&surface, &args(&["connect", flag])),
EXIT_OK,
"proxy connect {flag} must succeed"
);
assert_eq!(
run_proxy(&surface, &args(&["init", flag])),
EXIT_OK,
"proxy init {flag} must succeed"
);
}
}

#[test]
fn help_flag_positions_for_other_domains_are_unchanged() {
let queue = domain("queue").expect("queue domain exists");
Expand Down