arcbox-cli 0.4.9

Command-line interface for ArcBox
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//! Shell integration setup commands.
//!
//! Manages CLI registration into the user's PATH and shell completions so that
//! `abctl` is available from any terminal session.

use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use clap::{CommandFactory, Subcommand, ValueEnum};
use serde::Serialize;

use super::{Cli, OutputFormat};

/// Shell integration setup commands.
#[derive(Subcommand)]
pub enum SetupCommands {
    /// Install shell integration (PATH, completions, profile)
    Install,

    /// Remove shell integration
    Uninstall,

    /// Check installation status
    Status,

    /// Print shell completions to stdout
    Completions(CompletionsArgs),
}

/// Arguments for the completions subcommand.
#[derive(clap::Args)]
pub struct CompletionsArgs {
    /// Target shell
    #[arg(long, value_enum)]
    pub shell: ShellKind,
}

/// Supported shells.
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ShellKind {
    Zsh,
    Bash,
    Fish,
}

// =============================================================================
// JSON output structures
// =============================================================================

/// Status report for `abctl setup status --format json`.
#[derive(Serialize)]
struct StatusOutput {
    installed: bool,
    bin_symlink: ComponentStatus,
    shell_init: ComponentStatus,
    profile_injected: ComponentStatus,
    completions: ComponentStatus,
    docker_plugins: ComponentStatus,
}

/// Per-component installation status.
#[derive(Serialize)]
struct ComponentStatus {
    ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    detail: Option<String>,
}

// =============================================================================
// Path helpers
// =============================================================================

/// Root directory for ArcBox shell integration files (`~/.arcbox/`).
fn arcbox_home() -> Result<PathBuf> {
    dirs::home_dir()
        .map(|h| h.join(".arcbox"))
        .context("could not determine home directory")
}

fn bin_dir() -> Result<PathBuf> {
    arcbox_home().map(|h| h.join("bin"))
}

fn shell_dir() -> Result<PathBuf> {
    arcbox_home().map(|h| h.join("shell"))
}

fn completions_dir() -> Result<PathBuf> {
    arcbox_home().map(|h| h.join("completions"))
}

// =============================================================================
// Command dispatch
// =============================================================================

/// Execute setup commands.
pub async fn execute(command: SetupCommands, format: OutputFormat) -> Result<()> {
    match command {
        SetupCommands::Install => install(format).await,
        SetupCommands::Uninstall => uninstall(format).await,
        SetupCommands::Status => status(format).await,
        SetupCommands::Completions(args) => {
            print_completions(args.shell);
            Ok(())
        }
    }
}

// =============================================================================
// Install
// =============================================================================

async fn install(format: OutputFormat) -> Result<()> {
    let bin = bin_dir()?;
    let shell = shell_dir()?;
    let comp = completions_dir()?;

    // 1. Create directories.
    tokio::fs::create_dir_all(&bin).await?;
    tokio::fs::create_dir_all(&shell).await?;
    tokio::fs::create_dir_all(comp.join("zsh")).await?;
    tokio::fs::create_dir_all(comp.join("bash")).await?;
    tokio::fs::create_dir_all(comp.join("fish")).await?;

    // 2. Symlink current executable → ~/.arcbox/bin/abctl (primary).
    //    Also create ~/.arcbox/bin/arcbox → placeholder for backwards compat.
    let exe = std::env::current_exe().context("could not determine current executable path")?;
    let exe_dir = exe
        .parent()
        .context("could not determine executable directory")?;
    let symlink_path = bin.join("abctl");
    create_or_update_symlink(&exe, &symlink_path).await?;

    // The placeholder binary lives next to the main binary.
    let placeholder_exe = exe_dir.join("arcbox");
    let placeholder_symlink = bin.join("arcbox");
    if placeholder_exe.exists() {
        create_or_update_symlink(&placeholder_exe, &placeholder_symlink).await?;
    }

    // 2b. Symlink Docker CLI tools → ~/.arcbox/bin/ if available.
    //     Tools may be in the app bundle (xbin/) or ~/.arcbox/runtime/bin/.
    let docker_tools_linked = link_docker_tools_to_user_bin(&exe, &bin).await;

    // 2c. Register docker-compose / docker-buildx as Docker CLI plugins so
    //     `docker compose` (space-separated) and `docker buildx` resolve
    //     the same binaries as `docker-compose` / `docker-buildx` do on
    //     $PATH. See `cli_plugins` module docs for the rationale.
    //     Non-fatal: a failure here doesn't block the rest of install, but
    //     we capture the error so the user can see why plugins are missing
    //     instead of silently reporting 0 registered.
    let (plugins_registered, plugin_error) = match super::cli_plugins::default_docker_config_dir() {
        Ok(docker_cfg) => match super::cli_plugins::register(&bin, &docker_cfg).await {
            Ok(o) => (o, None),
            Err(e) => (
                super::cli_plugins::Outcome::default(),
                Some(format!("{e:#}")),
            ),
        },
        Err(e) => (
            super::cli_plugins::Outcome::default(),
            Some(format!("{e:#}")),
        ),
    };

    // 3. Write shell init scripts.
    write_shell_init_scripts(&shell).await?;

    // 4. Generate completions.
    generate_all_completions(&comp)?;

    // 5. Inject into shell profile.
    let detected_shell = detect_shell();
    let profile_path = inject_profile(detected_shell).await?;

    match format {
        OutputFormat::Json => {
            println!(
                "{}",
                serde_json::to_string(&serde_json::json!({
                    "installed": true,
                    "bin": symlink_path.display().to_string(),
                    "docker_tools": docker_tools_linked,
                    "docker_plugins": plugins_registered,
                    "docker_plugins_error": plugin_error,
                    "shell_init": shell.display().to_string(),
                    "completions": comp.display().to_string(),
                    "profile": profile_path.as_ref().map(|p| p.display().to_string()),
                }))?
            );
        }
        OutputFormat::Quiet => {}
        OutputFormat::Table => {
            println!("ArcBox CLI Setup");
            println!("================");
            println!();
            println!(
                "  Symlink:     {} -> {}",
                symlink_path.display(),
                exe.display()
            );
            if docker_tools_linked > 0 {
                println!(
                    "  Docker:      {docker_tools_linked} tools linked to {}",
                    bin.display()
                );
            }
            let plugin_count = plugins_registered.symlinks.len();
            if plugin_count > 0 || plugins_registered.config_updated {
                println!(
                    "  CLI plugins: {plugin_count} registered (`docker compose` / `docker buildx`)"
                );
            }
            if let Some(ref err) = plugin_error {
                println!("  CLI plugins: WARN: {err}");
            }
            println!("  Shell init:  {}", shell.display());
            println!("  Completions: {}", comp.display());
            if let Some(ref p) = profile_path {
                println!("  Profile:     {} (updated)", p.display());
            }
            println!();
            println!("Restart your shell or run:");
            println!("  source {}/init.zsh", shell.display());
        }
    }

    Ok(())
}

// =============================================================================
// Uninstall
// =============================================================================

async fn uninstall(format: OutputFormat) -> Result<()> {
    let bin = bin_dir()?;
    let shell = shell_dir()?;
    let comp = completions_dir()?;

    // Unregister Docker CLI plugins first — while `bin` still exists, so the
    // symlink-target ownership check can resolve. Idempotent and non-fatal,
    // but we capture any error so it surfaces in output rather than vanishing.
    let (plugins_unregistered, plugin_error) = match super::cli_plugins::default_docker_config_dir()
    {
        Ok(docker_cfg) => match super::cli_plugins::unregister(&bin, &docker_cfg).await {
            Ok(o) => (o, None),
            Err(e) => (
                super::cli_plugins::Outcome::default(),
                Some(format!("{e:#}")),
            ),
        },
        Err(e) => (
            super::cli_plugins::Outcome::default(),
            Some(format!("{e:#}")),
        ),
    };

    // Remove directories.
    remove_dir_if_exists(&bin).await;
    remove_dir_if_exists(&shell).await;
    remove_dir_if_exists(&comp).await;

    // Remove profile injection.
    let detected_shell = detect_shell();
    let removed_from = remove_profile_injection(detected_shell).await?;

    match format {
        OutputFormat::Json => {
            println!(
                "{}",
                serde_json::to_string(&serde_json::json!({
                    "uninstalled": true,
                    "docker_plugins": plugins_unregistered,
                    "docker_plugins_error": plugin_error,
                    "profile_cleaned": removed_from.as_ref().map(|p| p.display().to_string()),
                }))?
            );
        }
        OutputFormat::Quiet => {}
        OutputFormat::Table => {
            println!("ArcBox CLI shell integration removed.");
            if !plugins_unregistered.symlinks.is_empty() || plugins_unregistered.config_updated {
                println!(
                    "  CLI plugins:  {} symlinks removed",
                    plugins_unregistered.symlinks.len()
                );
            }
            if let Some(ref err) = plugin_error {
                println!("  CLI plugins:  WARN: {err}");
            }
            if let Some(ref p) = removed_from {
                println!("  Cleaned profile: {}", p.display());
            }
            println!("  Restart your shell to apply changes.");
        }
    }

    Ok(())
}

// =============================================================================
// Status
// =============================================================================

async fn status(format: OutputFormat) -> Result<()> {
    let bin = bin_dir()?;
    let shell = shell_dir()?;
    let comp = completions_dir()?;

    let symlink_path = bin.join("abctl");
    let symlink_ok = tokio::fs::symlink_metadata(&symlink_path)
        .await
        .is_ok_and(|m| m.file_type().is_symlink());
    let symlink_target = if symlink_ok {
        tokio::fs::read_link(&symlink_path)
            .await
            .ok()
            .map(|p| p.display().to_string())
    } else {
        None
    };

    let detected_shell = detect_shell();
    let init_script = shell_init_path(&shell, detected_shell);
    let init_ok = tokio::fs::metadata(&init_script).await.is_ok();

    let profile = profile_path(detected_shell);
    let profile_injected = if let Some(ref p) = profile {
        check_profile_injected(p).await
    } else {
        false
    };

    let zsh_comp = comp.join("zsh/_abctl");
    let comp_ok = tokio::fs::metadata(&zsh_comp).await.is_ok();

    // Docker CLI plugin registration — only considered "ok" if at least
    // one of our plugin binaries is on disk and is actually registered. If
    // none are present (e.g. developer CLI-only build), report as
    // not-applicable rather than failing.
    let any_plugin_present = arcbox_constants::paths::DOCKER_CLI_PLUGINS
        .iter()
        .any(|p| bin.join(p).exists());
    let (plugin_status, plugin_detail) = match super::cli_plugins::default_docker_config_dir() {
        Ok(docker_cfg) => {
            let st = super::cli_plugins::status(&bin, &docker_cfg).await;
            let ok =
                !any_plugin_present || (!st.symlinked.is_empty() || st.extra_dirs_entry_present);
            let detail = if any_plugin_present {
                Some(format!(
                    "{} symlinks, extraDirs: {}",
                    st.symlinked.len(),
                    if st.extra_dirs_entry_present {
                        "yes"
                    } else {
                        "no"
                    }
                ))
            } else {
                Some("skipped (no Docker CLI plugin binaries present)".to_string())
            };
            (ok, detail)
        }
        Err(_) => (true, Some("skipped (no home directory)".to_string())),
    };

    let all_ok = symlink_ok && init_ok && profile_injected && comp_ok && plugin_status;

    match format {
        OutputFormat::Json => {
            let output = StatusOutput {
                installed: all_ok,
                bin_symlink: ComponentStatus {
                    ok: symlink_ok,
                    path: Some(symlink_path.display().to_string()),
                    detail: symlink_target,
                },
                shell_init: ComponentStatus {
                    ok: init_ok,
                    path: Some(init_script.display().to_string()),
                    detail: None,
                },
                profile_injected: ComponentStatus {
                    ok: profile_injected,
                    path: profile.as_ref().map(|p| p.display().to_string()),
                    detail: None,
                },
                completions: ComponentStatus {
                    ok: comp_ok,
                    path: Some(comp.display().to_string()),
                    detail: None,
                },
                docker_plugins: ComponentStatus {
                    ok: plugin_status,
                    path: None,
                    detail: plugin_detail,
                },
            };
            println!("{}", serde_json::to_string(&output)?);
        }
        OutputFormat::Quiet => {}
        OutputFormat::Table => {
            println!("ArcBox CLI Setup Status");
            println!("=======================");
            println!();
            print_check(
                "CLI symlink",
                symlink_ok,
                &symlink_path.display().to_string(),
            );
            print_check("Shell init", init_ok, &init_script.display().to_string());
            print_check(
                "Profile injection",
                profile_injected,
                &profile
                    .as_ref()
                    .map(|p| p.display().to_string())
                    .unwrap_or_default(),
            );
            print_check("Completions", comp_ok, &comp.display().to_string());
            print_check(
                "Docker plugins",
                plugin_status,
                plugin_detail.as_deref().unwrap_or(""),
            );
            println!();
            if all_ok {
                println!("Status: installed");
            } else {
                println!("Status: not installed (run `abctl setup install`)");
            }
        }
    }

    Ok(())
}

fn print_check(label: &str, ok: bool, detail: &str) {
    let icon = if ok { "+" } else { "-" };
    println!("  [{}] {:<20} {}", icon, label, detail);
}

// =============================================================================
// Completions
// =============================================================================

fn print_completions(shell: ShellKind) {
    let mut cmd = Cli::command();
    let clap_shell = to_clap_shell(shell);
    clap_complete::generate(clap_shell, &mut cmd, "abctl", &mut std::io::stdout());
}

fn generate_all_completions(comp_dir: &Path) -> Result<()> {
    let shells = [
        (clap_complete::Shell::Zsh, comp_dir.join("zsh/_abctl")),
        (clap_complete::Shell::Bash, comp_dir.join("bash/abctl")),
        (clap_complete::Shell::Fish, comp_dir.join("fish/abctl.fish")),
    ];

    for (shell, path) in &shells {
        let mut cmd = Cli::command();
        let mut buf = Vec::new();
        clap_complete::generate(*shell, &mut cmd, "abctl", &mut buf);
        std::fs::write(path, buf)
            .with_context(|| format!("failed to write completions to {}", path.display()))?;
    }

    Ok(())
}

fn to_clap_shell(shell: ShellKind) -> clap_complete::Shell {
    match shell {
        ShellKind::Zsh => clap_complete::Shell::Zsh,
        ShellKind::Bash => clap_complete::Shell::Bash,
        ShellKind::Fish => clap_complete::Shell::Fish,
    }
}

// =============================================================================
// Shell init scripts
// =============================================================================

async fn write_shell_init_scripts(shell_dir: &Path) -> Result<()> {
    let zsh = r#"# ArcBox shell integration (zsh)
# This file is auto-generated by `abctl setup install`.
export PATH="${HOME}/.arcbox/bin:${PATH}"
fpath+=("${HOME}/.arcbox/completions/zsh")
"#;

    let bash = r#"# ArcBox shell integration (bash)
# This file is auto-generated by `abctl setup install`.
export PATH="${HOME}/.arcbox/bin:${PATH}"
for _abctl_comp in "${HOME}"/.arcbox/completions/bash/*; do
    [ -f "$_abctl_comp" ] && source "$_abctl_comp"
done
unset _abctl_comp
"#;

    let fish = "# ArcBox shell integration (fish)
# This file is auto-generated by `abctl setup install`.
fish_add_path -gP ~/.arcbox/bin
for f in ~/.arcbox/completions/fish/*.fish
    source $f 2>/dev/null
end
";

    tokio::fs::write(shell_dir.join("init.zsh"), zsh).await?;
    tokio::fs::write(shell_dir.join("init.bash"), bash).await?;
    tokio::fs::write(shell_dir.join("init.fish"), fish).await?;

    Ok(())
}

// =============================================================================
// Profile injection
// =============================================================================

/// Marker comment used to identify our injected lines.
const PROFILE_MARKER: &str = "# Added by ArcBox: command-line tools and integration";

fn detect_shell() -> ShellKind {
    std::env::var("SHELL")
        .ok()
        .and_then(|s| {
            if s.contains("zsh") {
                Some(ShellKind::Zsh)
            } else if s.contains("fish") {
                Some(ShellKind::Fish)
            } else if s.contains("bash") {
                Some(ShellKind::Bash)
            } else {
                None
            }
        })
        .unwrap_or(ShellKind::Zsh)
}

fn profile_path(shell: ShellKind) -> Option<PathBuf> {
    dirs::home_dir().map(|home| match shell {
        ShellKind::Zsh => home.join(".zprofile"),
        ShellKind::Bash => home.join(".bash_profile"),
        ShellKind::Fish => home.join(".config/fish/config.fish"),
    })
}

fn shell_init_path(shell_dir: &Path, shell: ShellKind) -> PathBuf {
    match shell {
        ShellKind::Zsh => shell_dir.join("init.zsh"),
        ShellKind::Bash => shell_dir.join("init.bash"),
        ShellKind::Fish => shell_dir.join("init.fish"),
    }
}

fn source_line(shell: ShellKind) -> String {
    match shell {
        ShellKind::Zsh => {
            format!("{PROFILE_MARKER}\nsource ~/.arcbox/shell/init.zsh 2>/dev/null || :")
        }
        ShellKind::Bash => {
            format!("{PROFILE_MARKER}\nsource ~/.arcbox/shell/init.bash 2>/dev/null || :")
        }
        ShellKind::Fish => {
            format!("{PROFILE_MARKER}\nsource ~/.arcbox/shell/init.fish 2>/dev/null; or true")
        }
    }
}

async fn check_profile_injected(path: &Path) -> bool {
    tokio::fs::read_to_string(path)
        .await
        .is_ok_and(|content| content.contains(PROFILE_MARKER))
}

/// Inject the source line into the user's shell profile. Returns the path if
/// modified.
async fn inject_profile(shell: ShellKind) -> Result<Option<PathBuf>> {
    let Some(path) = profile_path(shell) else {
        return Ok(None);
    };

    if check_profile_injected(&path).await {
        return Ok(Some(path));
    }

    // Ensure parent directory exists (for fish: ~/.config/fish/).
    if let Some(parent) = path.parent() {
        tokio::fs::create_dir_all(parent).await?;
    }

    let existing = tokio::fs::read_to_string(&path).await.unwrap_or_default();
    let separator = if existing.is_empty() || existing.ends_with('\n') {
        ""
    } else {
        "\n"
    };
    let snippet = source_line(shell);
    let new_content = format!("{existing}{separator}\n{snippet}\n");
    tokio::fs::write(&path, new_content).await?;

    Ok(Some(path))
}

/// Remove our injected lines from the user's shell profile.
async fn remove_profile_injection(shell: ShellKind) -> Result<Option<PathBuf>> {
    let Some(path) = profile_path(shell) else {
        return Ok(None);
    };

    let content = match tokio::fs::read_to_string(&path).await {
        Ok(c) => c,
        Err(_) => return Ok(None),
    };

    if !content.contains(PROFILE_MARKER) {
        return Ok(None);
    }

    // Remove all lines that are part of our injection block.
    let cleaned: Vec<&str> = content
        .lines()
        .filter(|line| !line.contains(PROFILE_MARKER) && !line.contains(".arcbox/shell/init."))
        .collect();

    // Trim trailing blank lines that our removal may have left.
    let mut result = cleaned.join("\n");
    while result.ends_with("\n\n") {
        result.pop();
    }
    if !result.is_empty() && !result.ends_with('\n') {
        result.push('\n');
    }

    tokio::fs::write(&path, result).await?;

    Ok(Some(path))
}

/// Links Docker tools into `~/.arcbox/bin/` (user-space, no root).
///
/// The system-wide `/usr/local/bin/*` path is handled by `arcbox-helper` with
/// ownership checks. Here we write into `~/.arcbox/bin/` which is ArcBox-owned,
/// so `create_or_update_symlink` (unconditional replace) is safe.
///
/// Searches xbin (app bundle) first, then `~/.arcbox/runtime/bin/` (daemon).
async fn link_docker_tools_to_user_bin(abctl_exe: &Path, user_bin: &Path) -> usize {
    let mut candidates: Vec<PathBuf> = Vec::new();

    // 1. App bundle xbin (reuses shared detection logic).
    if let Some(xbin) = super::symlink::detect_bundle_xbin() {
        candidates.push(xbin);
    } else if let Some(bin_dir) = abctl_exe.parent() {
        // Fallback: derive from exe path (e.g. Homebrew layout).
        if let Some(xbin) = bin_dir.parent().map(|p| p.join("xbin")) {
            if xbin.is_dir() {
                candidates.push(xbin);
            }
        }
    }

    // 2. Runtime bin: ~/.arcbox/runtime/bin/ (populated by daemon).
    if let Some(home) = dirs::home_dir() {
        let runtime_bin = home.join(".arcbox/runtime/bin");
        if runtime_bin.is_dir() {
            candidates.push(runtime_bin);
        }
    }

    let mut linked = 0usize;
    for tool_name in arcbox_constants::paths::DOCKER_CLI_TOOLS {
        let link = user_bin.join(tool_name);

        // Skip if already a valid symlink pointing to an existing target.
        if let Ok(meta) = tokio::fs::symlink_metadata(&link).await {
            if meta.file_type().is_symlink() {
                if let Ok(target) = tokio::fs::read_link(&link).await {
                    if target.exists() {
                        linked += 1;
                        continue;
                    }
                }
            }
        }

        for src_dir in &candidates {
            let src = src_dir.join(tool_name);
            if src.is_file() {
                if create_or_update_symlink(&src, &link).await.is_ok() {
                    linked += 1;
                }
                break;
            }
        }
    }

    linked
}

// =============================================================================
// Symlink helpers
// =============================================================================

/// Create or update a symlink, removing any stale one first.
async fn create_or_update_symlink(target: &Path, link: &Path) -> Result<()> {
    // Remove existing symlink or file.
    if tokio::fs::symlink_metadata(link).await.is_ok() {
        tokio::fs::remove_file(link).await.ok();
    }

    #[cfg(unix)]
    tokio::fs::symlink(target, link).await.with_context(|| {
        format!(
            "failed to create symlink {} -> {}",
            link.display(),
            target.display()
        )
    })?;

    Ok(())
}

/// Remove a directory if it exists, ignoring errors.
async fn remove_dir_if_exists(path: &Path) {
    let _ = tokio::fs::remove_dir_all(path).await;
}

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

    #[test]
    fn detect_zsh_from_env() {
        // detect_shell() reads $SHELL — just verify the function doesn't panic.
        let _ = detect_shell();
    }

    #[test]
    fn source_lines_contain_marker() {
        for shell in [ShellKind::Zsh, ShellKind::Bash, ShellKind::Fish] {
            let line = source_line(shell);
            assert!(line.contains(PROFILE_MARKER));
            assert!(line.contains(".arcbox/shell/init."));
        }
    }

    #[tokio::test]
    async fn profile_injection_is_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        let profile = dir.path().join(".zprofile");
        tokio::fs::write(&profile, "# existing content\n")
            .await
            .unwrap();

        // Inject twice by manually calling the injection logic.
        let snippet = source_line(ShellKind::Zsh);

        // First injection.
        let content = tokio::fs::read_to_string(&profile).await.unwrap();
        assert!(!content.contains(PROFILE_MARKER));
        let new = format!("{content}\n{snippet}\n");
        tokio::fs::write(&profile, &new).await.unwrap();

        // Verify marker is present.
        assert!(check_profile_injected(&profile).await);

        // Second injection should be a no-op (check_profile_injected returns true).
        assert!(check_profile_injected(&profile).await);
    }

    #[test]
    fn completions_generate_without_panic() {
        let dir = tempfile::tempdir().unwrap();
        let comp_dir = dir.path();
        std::fs::create_dir_all(comp_dir.join("zsh")).unwrap();
        std::fs::create_dir_all(comp_dir.join("bash")).unwrap();
        std::fs::create_dir_all(comp_dir.join("fish")).unwrap();
        generate_all_completions(comp_dir).unwrap();

        assert!(comp_dir.join("zsh/_abctl").exists());
        assert!(comp_dir.join("bash/abctl").exists());
        assert!(comp_dir.join("fish/abctl.fish").exists());
    }
}