teamctl 0.7.2

Declarative CLI for running persistent AI agent teams.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! `teamctl update` — re-run the installer that brought teamctl in.
//!
//! Detects the install method from `current_exe()`:
//!
//! - `…/Cellar/teamctl/…` → Homebrew (`brew upgrade teamctl`).
//! - `…/.cargo/bin/teamctl` → cargo (`cargo install teamctl team-mcp team-bot --force`).
//! - Anything else → shell installer (`curl -fsSL https://teamctl.run/install | sh`).
//!
//! The user can override autodetect with `--method <name>` and skip the
//! "Update? [Y/n]" prompt with `--yes`. `--check` prints the version
//! comparison and exits without updating.

use std::path::Path;
use std::process::{Command, ExitStatus};

use anyhow::{anyhow, bail, Context, Result};

const INSTALL_URL: &str = "https://teamctl.run/install";
const RELEASES_API: &str = "https://api.github.com/repos/Alireza29675/teamctl/releases/latest";
pub const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallMethod {
    Shell,
    Homebrew,
    Cargo,
}

impl InstallMethod {
    pub fn as_str(self) -> &'static str {
        match self {
            InstallMethod::Shell => "shell",
            InstallMethod::Homebrew => "brew",
            InstallMethod::Cargo => "cargo",
        }
    }

    pub fn parse(s: &str) -> Result<Self> {
        match s.to_ascii_lowercase().as_str() {
            "shell" | "installer" | "curl" => Ok(InstallMethod::Shell),
            "brew" | "homebrew" => Ok(InstallMethod::Homebrew),
            "cargo" => Ok(InstallMethod::Cargo),
            other => bail!("unknown method `{other}` (expected: shell, brew, cargo)"),
        }
    }
}

pub fn run(method_override: Option<String>, check_only: bool, yes: bool) -> Result<()> {
    let exe = std::env::current_exe().context("locate current teamctl exe")?;
    let detected = detect_install_method(&exe);
    let method = match method_override.as_deref() {
        Some(s) => InstallMethod::parse(s)?,
        None => detected,
    };

    println!(
        "teamctl {CURRENT_VERSION} ({} install, exe: {})",
        method.as_str(),
        exe.display()
    );

    let latest = fetch_latest_version()
        .with_context(|| format!("fetch latest version from {RELEASES_API}"))?;
    let cmp = compare_versions(CURRENT_VERSION, &latest);

    match cmp {
        VersionOrder::Equal => {
            println!("✓ already on the latest version ({latest}).");
            return Ok(());
        }
        VersionOrder::Newer => {
            println!(
                "Local version {CURRENT_VERSION} is ahead of the latest published \
                 release ({latest}) — nothing to update."
            );
            return Ok(());
        }
        VersionOrder::Older => {
            println!("→ update available: {CURRENT_VERSION}{latest}");
        }
    }

    if check_only {
        return Ok(());
    }

    let plan = plan_for(method);
    println!("Plan: {}", plan.describe());

    if !yes && !confirm("Proceed? [Y/n] ", true)? {
        println!("  cancelled");
        return Ok(());
    }

    plan.execute()?;
    println!("✓ update complete. Run `teamctl --version` to confirm.");
    Ok(())
}

// ── Detection ───────────────────────────────────────────────────────

/// Pick an install method from the path of the running `teamctl` exe.
/// Robust to both macOS (`/opt/homebrew/Cellar/...`) and Linux brew
/// (`/home/linuxbrew/...`) layouts. Returns Shell for anything we
/// don't recognise — that's also what the install.sh-managed
/// `~/.local/bin` path looks like.
pub fn detect_install_method(exe: &Path) -> InstallMethod {
    let p = exe.to_string_lossy();
    if p.contains("/Cellar/teamctl/") || p.contains("/linuxbrew/") || p.contains("/homebrew/") {
        return InstallMethod::Homebrew;
    }
    if p.contains("/.cargo/bin/") || p.contains("/cargo/bin/") {
        return InstallMethod::Cargo;
    }
    InstallMethod::Shell
}

// ── Latest-version probe ────────────────────────────────────────────

fn fetch_latest_version() -> Result<String> {
    // GitHub's releases API returns JSON. We only need `tag_name`, and
    // the response can be either pretty-printed (line per field) or a
    // single 80kB blob, so we scan the body as a whole string rather
    // than line-by-line.
    let body = curl_get(RELEASES_API)?;
    extract_tag_name(&body)
        .ok_or_else(|| anyhow!("no `tag_name` field in GitHub releases response"))
}

/// Pull the `tag_name` value out of a GitHub releases-API JSON blob.
/// Returns `None` when the field isn't present or is empty. Strips a
/// leading `v` so callers can compare directly against `Cargo.toml`'s
/// version.
fn extract_tag_name(body: &str) -> Option<String> {
    let needle = "\"tag_name\":";
    let idx = body.find(needle)?;
    let after = &body[idx + needle.len()..];
    let after = after.trim_start();
    let value = after.strip_prefix('"')?;
    let end = value.find('"')?;
    let tag = value[..end].trim().trim_start_matches('v').to_string();
    if tag.is_empty() {
        None
    } else {
        Some(tag)
    }
}

#[derive(Debug, PartialEq, Eq)]
enum VersionOrder {
    Older,
    Equal,
    Newer,
}

/// Lexicographic semver compare for `MAJOR.MINOR.PATCH`. Pre-release
/// suffixes (e.g. `-rc.1`) are stripped before comparison; we treat
/// them as equal to the base version for update-prompt purposes,
/// because anyone running a pre-release knowingly opted in.
fn compare_versions(local: &str, latest: &str) -> VersionOrder {
    let l = parse_triplet(local);
    let r = parse_triplet(latest);
    match l.cmp(&r) {
        std::cmp::Ordering::Less => VersionOrder::Older,
        std::cmp::Ordering::Equal => VersionOrder::Equal,
        std::cmp::Ordering::Greater => VersionOrder::Newer,
    }
}

fn parse_triplet(v: &str) -> (u32, u32, u32) {
    let core = v.split('-').next().unwrap_or(v).trim_start_matches('v');
    let mut it = core.split('.');
    let major = it.next().and_then(|s| s.parse().ok()).unwrap_or(0);
    let minor = it.next().and_then(|s| s.parse().ok()).unwrap_or(0);
    let patch = it.next().and_then(|s| s.parse().ok()).unwrap_or(0);
    (major, minor, patch)
}

// ── Install plan ────────────────────────────────────────────────────

struct Plan {
    method: InstallMethod,
}

impl Plan {
    fn describe(&self) -> String {
        match self.method {
            InstallMethod::Shell => format!("re-run the shell installer (curl {INSTALL_URL} | sh)"),
            InstallMethod::Homebrew => "brew upgrade teamctl".to_string(),
            InstallMethod::Cargo => "cargo install teamctl team-mcp team-bot --force".to_string(),
        }
    }

    fn execute(&self) -> Result<()> {
        match self.method {
            InstallMethod::Shell => exec_shell_installer(),
            InstallMethod::Homebrew => exec_brew_upgrade(),
            InstallMethod::Cargo => exec_cargo_install(),
        }
    }
}

fn plan_for(method: InstallMethod) -> Plan {
    Plan { method }
}

fn exec_shell_installer() -> Result<()> {
    require_on_path("curl")?;
    require_on_path("sh")?;
    // We pipe curl into sh via a single shell invocation so the
    // installer's progress output streams to the user in real time.
    let cmd = format!("curl -fsSL {INSTALL_URL} | sh");
    let status = Command::new("sh")
        .args(["-c", &cmd])
        .status()
        .context("run shell installer")?;
    require_success(status, "shell installer")
}

fn exec_brew_upgrade() -> Result<()> {
    require_on_path("brew")?;
    // `brew update` first so the formula bump from cargo-dist's
    // homebrew tap is picked up; otherwise `brew upgrade` may report
    // "already up-to-date" against a stale tap.
    let status = Command::new("brew")
        .args(["update"])
        .status()
        .context("run `brew update`")?;
    require_success(status, "brew update")?;
    let status = Command::new("brew")
        .args(["upgrade", "teamctl"])
        .status()
        .context("run `brew upgrade teamctl`")?;
    require_success(status, "brew upgrade teamctl")
}

fn exec_cargo_install() -> Result<()> {
    require_on_path("cargo")?;
    let status = Command::new("cargo")
        .args(["install", "teamctl", "team-mcp", "team-bot", "--force"])
        .status()
        .context("run cargo install")?;
    require_success(status, "cargo install")
}

fn require_on_path(bin: &str) -> Result<()> {
    if which(bin).is_some() {
        return Ok(());
    }
    bail!("`{bin}` not found on PATH — install it and re-run, or pick another method with --method")
}

fn which(bin: &str) -> Option<std::path::PathBuf> {
    let path = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path) {
        let candidate = dir.join(bin);
        if candidate.is_file() {
            return Some(candidate);
        }
        #[cfg(windows)]
        {
            let exe_candidate = candidate.with_extension("exe");
            if exe_candidate.is_file() {
                return Some(exe_candidate);
            }
        }
    }
    None
}

fn require_success(status: ExitStatus, label: &str) -> Result<()> {
    if status.success() {
        return Ok(());
    }
    bail!(
        "{label} exited with status {} — see output above",
        status
            .code()
            .map(|c| c.to_string())
            .unwrap_or_else(|| "(killed by signal)".into())
    )
}

fn curl_get(url: &str) -> Result<String> {
    // Mirror the helper in cmd::bot — keeps deps minimal. GitHub's API
    // requires a User-Agent header, so we set one explicitly; without
    // it we get a 403 even for unauthenticated read endpoints.
    let out = Command::new("curl")
        .args([
            "-sS",
            "-H",
            &format!("User-Agent: teamctl-cli/{CURRENT_VERSION}"),
            "--max-time",
            "15",
            url,
        ])
        .output()
        .context("run curl (is curl installed?)")?;
    if !out.status.success() {
        let err = String::from_utf8_lossy(&out.stderr);
        bail!("curl failed: {}", err.trim());
    }
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

fn confirm(msg: &str, default_yes: bool) -> Result<bool> {
    use std::io::{self, BufRead, Write};
    print!("{msg}");
    io::stdout().flush().ok();
    let mut line = String::new();
    io::stdin()
        .lock()
        .read_line(&mut line)
        .context("read stdin")?;
    let raw = line.trim().to_lowercase();
    if raw.is_empty() {
        return Ok(default_yes);
    }
    Ok(matches!(raw.as_str(), "y" | "yes"))
}

// ── Tests ───────────────────────────────────────────────────────────

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

    #[test]
    fn detect_homebrew_macos_layout() {
        let exe = PathBuf::from("/opt/homebrew/Cellar/teamctl/0.6.0/bin/teamctl");
        assert_eq!(detect_install_method(&exe), InstallMethod::Homebrew);
    }

    #[test]
    fn detect_homebrew_intel_layout() {
        let exe = PathBuf::from("/usr/local/Cellar/teamctl/0.6.0/bin/teamctl");
        assert_eq!(detect_install_method(&exe), InstallMethod::Homebrew);
    }

    #[test]
    fn detect_homebrew_linux_layout() {
        let exe = PathBuf::from("/home/linuxbrew/.linuxbrew/bin/teamctl");
        assert_eq!(detect_install_method(&exe), InstallMethod::Homebrew);
    }

    #[test]
    fn detect_cargo_layout() {
        let exe = PathBuf::from("/Users/alireza/.cargo/bin/teamctl");
        assert_eq!(detect_install_method(&exe), InstallMethod::Cargo);
    }

    #[test]
    fn detect_shell_default_for_local_bin() {
        let exe = PathBuf::from("/Users/alireza/.local/bin/teamctl");
        assert_eq!(detect_install_method(&exe), InstallMethod::Shell);
    }

    #[test]
    fn detect_shell_default_for_unknown_path() {
        let exe = PathBuf::from("/opt/teamctl/bin/teamctl");
        assert_eq!(detect_install_method(&exe), InstallMethod::Shell);
    }

    #[test]
    fn parse_method_accepts_synonyms() {
        assert_eq!(InstallMethod::parse("shell").unwrap(), InstallMethod::Shell);
        assert_eq!(InstallMethod::parse("curl").unwrap(), InstallMethod::Shell);
        assert_eq!(
            InstallMethod::parse("installer").unwrap(),
            InstallMethod::Shell
        );
        assert_eq!(
            InstallMethod::parse("brew").unwrap(),
            InstallMethod::Homebrew
        );
        assert_eq!(
            InstallMethod::parse("Homebrew").unwrap(),
            InstallMethod::Homebrew
        );
        assert_eq!(InstallMethod::parse("cargo").unwrap(), InstallMethod::Cargo);
    }

    #[test]
    fn parse_method_rejects_garbage() {
        assert!(InstallMethod::parse("snap").is_err());
        assert!(InstallMethod::parse("").is_err());
    }

    #[test]
    fn parse_triplet_handles_v_prefix_and_pre() {
        assert_eq!(parse_triplet("0.6.0"), (0, 6, 0));
        assert_eq!(parse_triplet("v0.6.0"), (0, 6, 0));
        assert_eq!(parse_triplet("1.2.3-rc.1"), (1, 2, 3));
        assert_eq!(parse_triplet("v10.20.30"), (10, 20, 30));
    }

    #[test]
    fn extract_tag_name_handles_single_line_json() {
        let blob = r#"{"id":1,"tag_name":"v0.6.0","name":"0.6.0"}"#;
        assert_eq!(extract_tag_name(blob).as_deref(), Some("0.6.0"));
    }

    #[test]
    fn extract_tag_name_handles_pretty_printed_json() {
        let blob = "{\n  \"id\": 1,\n  \"tag_name\": \"v0.5.1\",\n  \"name\": \"0.5.1\"\n}";
        assert_eq!(extract_tag_name(blob).as_deref(), Some("0.5.1"));
    }

    #[test]
    fn extract_tag_name_strips_v_prefix() {
        let blob = r#"{"tag_name":"v10.20.30"}"#;
        assert_eq!(extract_tag_name(blob).as_deref(), Some("10.20.30"));
    }

    #[test]
    fn extract_tag_name_returns_none_for_missing_field() {
        let blob = r#"{"message":"Not Found","status":"404"}"#;
        assert!(extract_tag_name(blob).is_none());
    }

    #[test]
    fn extract_tag_name_returns_none_for_empty_value() {
        let blob = r#"{"tag_name":""}"#;
        assert!(extract_tag_name(blob).is_none());
    }

    #[test]
    fn compare_versions_orders_correctly() {
        assert_eq!(compare_versions("0.5.1", "0.6.0"), VersionOrder::Older);
        assert_eq!(compare_versions("0.6.0", "0.6.0"), VersionOrder::Equal);
        assert_eq!(compare_versions("0.6.1", "0.6.0"), VersionOrder::Newer);
        assert_eq!(compare_versions("1.0.0", "0.99.99"), VersionOrder::Newer);
        // Pre-release suffix is stripped → equal to its base version.
        assert_eq!(compare_versions("0.6.0-rc.1", "0.6.0"), VersionOrder::Equal);
    }
}