From 3f709bed2ea5cd102b7d283997aa7f7976de593b Mon Sep 17 00:00:00 2001 From: Marco Arnulfo <103679419+marcoarnulfo@users.noreply.github.com> Date: Tue, 13 Oct 2026 10:37:52 +0200 Subject: [PATCH] Desktop: a bare --control keeps the CADCRAFT_CONTROL_PORT port (fixes #844) (#848) `--control` as the last argument read `args.next()` as None and reset the port taken from CADCRAFT_CONTROL_PORT, without logging anything, so `CADCRAFT_CONTROL_PORT=47321 cadcraft --control` started no control channel. A bare `--control` now keeps the variable's port and logs a warning either way; a port given on the command line still wins. The precedence is documented in docs/control-protocol.md. --- apps/cadcraft/src/main.rs | 27 +++++++++++++++++++++++++-- docs/control-protocol.md | 3 ++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/apps/cadcraft/src/main.rs b/apps/cadcraft/src/main.rs index 7101991a..34e2a7e8 100644 --- a/apps/cadcraft/src/main.rs +++ b/apps/cadcraft/src/main.rs @@ -203,6 +203,19 @@ fn control_port_from(source: &str, value: Option) -> Option { port } +/// `--control` followed by `value`: a bare `--control` (last argument) keeps the port already +/// read from `CADCRAFT_CONTROL_PORT` (`current`) instead of turning the control channel off. +fn control_flag(current: Option, value: Option) -> Option { + if value.is_some() { + return control_port_from("--control", value); + } + match current { + Some(port) => log::warn!("--control: no port given; using CADCRAFT_CONTROL_PORT ({port})"), + None => log::warn!("--control: no port given; the control channel is off"), + } + current +} + fn main() -> eframe::Result { // First, so every start-up record is captured; see `logging`. let logger = logging::install(); @@ -214,7 +227,7 @@ fn main() -> eframe::Result { let mut args = std::env::args().skip(1); while let Some(a) = args.next() { match a.as_str() { - "--control" => control_port = control_port_from("--control", args.next()), + "--control" => control_port = control_flag(control_port, args.next()), "--sample" => sample = true, "--version" | "-V" => { println!("{}", version_string()); @@ -347,7 +360,7 @@ fn main() -> eframe::Result { #[cfg(test)] mod tests { - use super::{config_dir_from, control_port_from, filters_for_save, follow_desktop}; + use super::{config_dir_from, control_flag, control_port_from, filters_for_save, follow_desktop}; use std::ffi::OsString; use std::path::PathBuf; @@ -381,6 +394,16 @@ mod tests { } } + #[test] + fn control_without_a_port_keeps_the_environment_port() { + // `CADCRAFT_CONTROL_PORT=47321 cadcraft --control` (#844) + assert_eq!(control_flag(Some(47321), None), Some(47321)); + assert_eq!(control_flag(None, None), None); + // A port given on the command line still wins over the variable. + assert_eq!(control_flag(Some(47321), Some("7979".into())), Some(7979)); + assert_eq!(control_flag(Some(47321), Some("abc".into())), None); + } + #[test] fn the_override_variable_wins_and_empty_values_are_unset() { let vars = [("CADCRAFT_CONFIG_DIR", "/tmp/cc"), ("HOME", "/home/u"), ("XDG_CONFIG_HOME", "/x"), ("APPDATA", "C:\\a")]; diff --git a/docs/control-protocol.md b/docs/control-protocol.md index daf91120..34cde6e3 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -1,7 +1,8 @@ # CADCraft control protocol Start the desktop app with `--control PORT` (or `CADCRAFT_CONTROL_PORT=PORT`). It listens on -`127.0.0.1:PORT` for JSON lines: one request per line, one reply per line. +`127.0.0.1:PORT` for JSON lines: one request per line, one reply per line. A port given with +`--control` wins over the variable; a bare `--control` keeps the variable's port. ```json {"id": 1, "method": "cmdline.input", "params": {"text": "circle 0,0 5"}}