Skip to main content

agentmux_launcher/
version_flag.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `agentmux --version` (or `-V`): print the version and exit 0, starting nothing.
5//!
6//! Without this the flag fell through to the normal start: the launcher brought up srv and the
7//! CEF host, whose own `--version` handling printed the Chromium version, so a person checking
8//! an install (the install page says to) saw no AgentMux version at all, and on Ubuntu the
9//! sandbox check stopped it first. The deb and rpm wrappers, the tarball's `agentmux.sh` and the
10//! AppImage's AppRun all `exec` this launcher with their arguments, so one check here covers
11//! them (AppRun answers it earlier still, before it unpacks anything).
12
13use std::io::Write;
14
15/// Only the first argument counts: `agentmux --version`, not a `--version` somewhere in a
16/// command line that means something else.
17fn requested(args: &[String]) -> bool {
18    matches!(args.get(1).map(String::as_str), Some("--version" | "-V"))
19}
20
21/// The one line printed. A local build also names its build label, which is how two builds of
22/// the same version tell apart; a release build's label is its version, so nothing is added.
23fn line(version: &str, label: Option<&str>) -> String {
24    match label.filter(|l| !l.is_empty() && *l != version) {
25        Some(label) => format!("AgentMux {version} (build {label})"),
26        None => format!("AgentMux {version}"),
27    }
28}
29
30/// `Some(exit code)` after printing the version when the arguments ask for it; `None` otherwise.
31pub fn run_if_requested(args: &[String]) -> Option<i32> {
32    if !requested(args) {
33        return None;
34    }
35    // A closed pipe (`agentmux --version | head -0`) is not an error worth a panic.
36    let _ = writeln!(
37        std::io::stdout(),
38        "{}",
39        line(env!("CARGO_PKG_VERSION"), option_env!("AGENTMUX_BUILD_LABEL"))
40    );
41    Some(0)
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    fn args(a: &[&str]) -> Vec<String> {
49        std::iter::once("agentmux").chain(a.iter().copied()).map(String::from).collect()
50    }
51
52    #[test]
53    fn long_and_short_flag_ask_for_the_version() {
54        assert!(requested(&args(&["--version"])));
55        assert!(requested(&args(&["-V"])));
56    }
57
58    #[test]
59    fn nothing_else_does() {
60        assert!(!requested(&args(&[])));
61        assert!(!requested(&args(&["--background"])));
62        assert!(!requested(&args(&["--diag", "sagas", "--version"])));
63        assert!(!requested(&args(&["--version-info"])));
64        assert!(!requested(&args(&["-v"])));
65    }
66
67    #[test]
68    fn a_release_prints_only_its_version() {
69        assert_eq!(line("0.60.21", None), "AgentMux 0.60.21");
70        assert_eq!(line("0.60.21", Some("")), "AgentMux 0.60.21");
71        assert_eq!(line("0.60.21", Some("0.60.21")), "AgentMux 0.60.21");
72    }
73
74    #[test]
75    fn a_local_build_adds_its_label() {
76        assert_eq!(line("0.60.21", Some("0.60.21-dev.1109")), "AgentMux 0.60.21 (build 0.60.21-dev.1109)");
77    }
78
79    #[test]
80    fn the_flag_exits_zero_and_other_arguments_do_not() {
81        assert_eq!(run_if_requested(&args(&["--version"])), Some(0));
82        assert_eq!(run_if_requested(&args(&["--background"])), None);
83    }
84}