cascade-cli 0.1.152

Stacked diffs CLI for Bitbucket Server
Documentation
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
use crate::cli::output::Output;
use crate::cli::Cli;
use crate::errors::{CascadeError, Result};
use clap::CommandFactory;
use clap_complete::{generate, Shell};
use std::fs;
use std::io;
use std::path::PathBuf;

/// Generate shell completions for the specified shell
pub fn generate_completions(shell: Shell) -> Result<()> {
    let mut cmd = Cli::command();
    let bin_name = "ca";

    generate(shell, &mut cmd, bin_name, &mut io::stdout());
    Ok(())
}

/// Install shell completions to the system
pub fn install_completions(shell: Option<Shell>) -> Result<()> {
    let shells_to_install = if let Some(shell) = shell {
        vec![shell]
    } else {
        // Detect current shell first, then fall back to available shells
        detect_current_and_available_shells()
    };

    let mut installed = Vec::new();
    let mut errors = Vec::new();

    for shell in shells_to_install {
        match install_completion_for_shell(shell) {
            Ok(path) => {
                installed.push((shell, path));
            }
            Err(e) => {
                errors.push((shell, e));
            }
        }
    }

    // Report results
    if !installed.is_empty() {
        Output::success("Shell completions installed:");
        for (shell, path) in &installed {
            Output::sub_item(format!("{:?}: {}", shell, path.display()));
        }

        println!();
        Output::tip("Next steps:");

        // Provide shell-specific setup instructions
        for (shell, path) in &installed {
            match shell {
                Shell::Zsh => {
                    if path.to_string_lossy().contains(".zsh/completions") {
                        println!();
                        Output::warning("⚠️  Zsh requires additional setup:");
                        Output::bullet("Add this to your ~/.zshrc:");
                        println!("      fpath=(~/.zsh/completions $fpath)");
                        println!("      autoload -Uz compinit && compinit");
                        Output::bullet("Then reload: source ~/.zshrc");
                    }
                }
                Shell::Bash => {
                    if path.to_string_lossy().contains(".bash_completion.d") {
                        println!();
                        Output::info("For bash completions to work:");
                        Output::bullet("Ensure bash-completion is installed");
                        Output::bullet("Then reload: source ~/.bashrc");
                    }
                }
                _ => {}
            }
        }

        println!();
        Output::bullet("Try: ca <TAB><TAB>");
    }

    if !errors.is_empty() {
        println!();
        Output::warning("Some installations failed:");
        for (shell, error) in errors {
            Output::sub_item(format!("{shell:?}: {error}"));
        }
    }

    Ok(())
}

/// Detect current shell first, then fall back to available shells
fn detect_current_and_available_shells() -> Vec<Shell> {
    let mut shells = Vec::new();

    // First, try to detect the current shell from SHELL environment variable
    if let Some(current_shell) = detect_current_shell() {
        shells.push(current_shell);
        Output::info(format!("Detected current shell: {current_shell:?}"));
        return shells; // Only install for current shell
    }

    // Fall back to detecting all available shells
    Output::info("Could not detect current shell, checking available shells...");
    detect_available_shells()
}

/// Detect the current shell from the SHELL environment variable
fn detect_current_shell() -> Option<Shell> {
    let shell_path = std::env::var("SHELL").ok()?;
    let shell_name = std::path::Path::new(&shell_path).file_name()?.to_str()?;

    match shell_name {
        "bash" => Some(Shell::Bash),
        "zsh" => Some(Shell::Zsh),
        "fish" => Some(Shell::Fish),
        _ => None,
    }
}

/// Detect which shells are available on the system
fn detect_available_shells() -> Vec<Shell> {
    let mut shells = Vec::new();

    // Check for bash
    if which_shell("bash").is_some() {
        shells.push(Shell::Bash);
    }

    // Check for zsh
    if which_shell("zsh").is_some() {
        shells.push(Shell::Zsh);
    }

    // Check for fish
    if which_shell("fish").is_some() {
        shells.push(Shell::Fish);
    }

    // Default to bash if nothing found
    if shells.is_empty() {
        shells.push(Shell::Bash);
    }

    shells
}

/// Check if a shell exists in PATH
fn which_shell(shell: &str) -> Option<PathBuf> {
    std::env::var("PATH")
        .ok()?
        .split(crate::utils::platform::path_separator())
        .map(PathBuf::from)
        .find_map(|path| {
            let shell_path = path.join(crate::utils::platform::executable_name(shell));
            if crate::utils::platform::is_executable(&shell_path) {
                Some(shell_path)
            } else {
                None
            }
        })
}

/// Install completion for a specific shell
fn install_completion_for_shell(shell: Shell) -> Result<PathBuf> {
    // Get platform-specific completion directories
    let completion_dirs = crate::utils::platform::shell_completion_dirs();

    let (completion_dir, filename) = match shell {
        Shell::Bash => {
            // Prioritize user directories over system directories
            let bash_dirs: Vec<_> = completion_dirs
                .iter()
                .filter(|(name, _)| name.contains("bash"))
                .collect();

            // First try user directories
            let user_dir = bash_dirs
                .iter()
                .find(|(name, _)| name.contains("user"))
                .map(|(_, path)| path.clone())
                .filter(|d| d.exists() || d.parent().is_some_and(|p| p.exists()));

            // If no user directory works, try system directories
            let system_dir = if user_dir.is_none() {
                bash_dirs
                    .iter()
                    .find(|(name, _)| name.contains("system"))
                    .map(|(_, path)| path.clone())
                    .filter(|d| d.exists() || d.parent().is_some_and(|p| p.exists()))
            } else {
                None
            };

            let dir = user_dir
                .or(system_dir)
                .or_else(|| {
                    // Fallback to user-specific directory
                    dirs::home_dir().map(|h| h.join(".bash_completion.d"))
                })
                .ok_or_else(|| {
                    CascadeError::config("Could not find suitable bash completion directory")
                })?;

            (dir, "ca")
        }
        Shell::Zsh => {
            // Prioritize user directories over system directories
            let zsh_dirs: Vec<_> = completion_dirs
                .iter()
                .filter(|(name, _)| name.contains("zsh"))
                .collect();

            // First try user directories
            let user_dir = zsh_dirs
                .iter()
                .find(|(name, _)| name.contains("user"))
                .map(|(_, path)| path.clone())
                .filter(|d| d.exists() || d.parent().is_some_and(|p| p.exists()));

            // If no user directory works, try system directories
            let system_dir = if user_dir.is_none() {
                zsh_dirs
                    .iter()
                    .find(|(name, _)| name.contains("system"))
                    .map(|(_, path)| path.clone())
                    .filter(|d| d.exists() || d.parent().is_some_and(|p| p.exists()))
            } else {
                None
            };

            let dir = user_dir
                .or(system_dir)
                .or_else(|| {
                    // Fallback to user-specific directory
                    dirs::home_dir().map(|h| h.join(".zsh/completions"))
                })
                .ok_or_else(|| {
                    CascadeError::config("Could not find suitable zsh completion directory")
                })?;

            (dir, "_ca")
        }
        Shell::Fish => {
            // Prioritize user directories over system directories
            let fish_dirs: Vec<_> = completion_dirs
                .iter()
                .filter(|(name, _)| name.contains("fish"))
                .collect();

            // First try user directories
            let user_dir = fish_dirs
                .iter()
                .find(|(name, _)| name.contains("user"))
                .map(|(_, path)| path.clone())
                .filter(|d| d.exists() || d.parent().is_some_and(|p| p.exists()));

            // If no user directory works, try system directories
            let system_dir = if user_dir.is_none() {
                fish_dirs
                    .iter()
                    .find(|(name, _)| name.contains("system"))
                    .map(|(_, path)| path.clone())
                    .filter(|d| d.exists() || d.parent().is_some_and(|p| p.exists()))
            } else {
                None
            };

            let dir = user_dir
                .or(system_dir)
                .or_else(|| {
                    // Fallback to user-specific directory
                    dirs::home_dir().map(|h| h.join(".config/fish/completions"))
                })
                .ok_or_else(|| {
                    CascadeError::config("Could not find suitable fish completion directory")
                })?;

            (dir, "ca.fish")
        }
        _ => {
            return Err(CascadeError::config(format!(
                "Unsupported shell: {shell:?}"
            )));
        }
    };

    // Create directory if it doesn't exist
    if !completion_dir.exists() {
        fs::create_dir_all(&completion_dir)?;
    }

    let completion_file =
        completion_dir.join(crate::utils::path_validation::sanitize_filename(filename));

    // Validate the completion file path for security
    crate::utils::path_validation::validate_config_path(&completion_file, &completion_dir)?;

    // Generate completion content
    let mut cmd = Cli::command();
    let mut content = Vec::new();
    generate(shell, &mut cmd, "ca", &mut content);

    // Add custom completion logic for stack names
    let custom_completion = generate_custom_completion(shell);
    if !custom_completion.is_empty() {
        content.extend_from_slice(custom_completion.as_bytes());
    }

    // Write to file atomically, with fallback for lock failures
    match crate::utils::atomic_file::write_bytes(&completion_file, &content) {
        Ok(()) => {}
        Err(e) if e.to_string().contains("Timeout waiting for lock") => {
            // Lock failure - try without locking for user directories
            if completion_dir.to_string_lossy().contains(
                &dirs::home_dir()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string(),
            ) {
                // This is a user directory, try direct write
                std::fs::write(&completion_file, &content)?;
            } else {
                // System directory, propagate the error
                return Err(e);
            }
        }
        Err(e) => return Err(e),
    }

    Ok(completion_file)
}

/// Show installation status and guidance
pub fn show_completions_status() -> Result<()> {
    Output::section("Shell Completions Status");

    let available_shells = detect_available_shells();

    Output::section("Available shells");
    for shell in &available_shells {
        let status = check_completion_installed(*shell);
        if status {
            Output::success(format!("{shell:?}"));
        } else {
            Output::error(format!("{shell:?}"));
        }
    }

    let all_installed = available_shells
        .iter()
        .all(|s| check_completion_installed(*s));

    if !all_installed {
        println!();
        Output::tip("To install completions:");
        Output::command_example("ca completions install");
        Output::command_example("ca completions install --shell bash  # for specific shell");
    } else {
        println!();
        Output::success("All available shells have completions installed!");

        // Check if zsh is available and provide setup instructions
        if available_shells.contains(&Shell::Zsh) {
            println!();

            // Check if zsh is already configured
            let zshrc_path = dirs::home_dir()
                .map(|h| h.join(".zshrc"))
                .unwrap_or_else(|| PathBuf::from("~/.zshrc"));

            let mut needs_fpath = true;
            let mut needs_compinit = true;

            let mut using_omz = false;
            let mut omz_line = None;

            if let Ok(zshrc_content) = std::fs::read_to_string(&zshrc_path) {
                // Check if Oh-My-Zsh is being used
                if zshrc_content.contains("oh-my-zsh.sh") {
                    using_omz = true;
                    // Find the line number where Oh-My-Zsh is sourced
                    for (i, line) in zshrc_content.lines().enumerate() {
                        if line.contains("source") && line.contains("oh-my-zsh.sh") {
                            omz_line = Some(i + 1);
                            break;
                        }
                    }
                }

                if zshrc_content.contains("fpath=(~/.zsh/completions")
                    || zshrc_content.contains("fpath=(\"$HOME/.zsh/completions\"")
                    || zshrc_content.contains("fpath=($HOME/.zsh/completions")
                {
                    needs_fpath = false;
                }
                if zshrc_content.contains("compinit") {
                    needs_compinit = false;
                }
            }

            if needs_fpath || needs_compinit {
                Output::warning("Zsh requires additional setup for completions to work");
                println!();

                if using_omz {
                    Output::sub_item("Detected Oh-My-Zsh - special setup required:");
                    println!();
                    if let Some(line_num) = omz_line {
                        Output::info(format!("Oh-My-Zsh loads at line {} in ~/.zshrc", line_num));
                        Output::sub_item("The fpath MUST be set BEFORE Oh-My-Zsh loads");
                        Output::sub_item(
                            "Oh-My-Zsh calls compinit internally, so DON'T add compinit yourself",
                        );
                        println!();
                    }

                    Output::sub_item("Option 1: Manual edit (recommended)");
                    Output::bullet("Open ~/.zshrc in an editor");
                    Output::bullet("Find the line: source $ZSH/oh-my-zsh.sh");
                    Output::bullet("Add this line BEFORE it:");
                    println!("      fpath=(~/.zsh/completions $fpath)");
                    Output::bullet("Make sure there's NO 'compinit' line at the end of ~/.zshrc");
                    Output::bullet("Save, then clear Oh-My-Zsh cache and reload:");
                    println!("      rm -f ~/.zcompdump && exec zsh");
                    println!();

                    Output::sub_item("Option 2: Automatic (requires sed)");
                    if let Some(line_num) = omz_line {
                        let insert_line = line_num;
                        Output::command_example(format!(
                            "sed -i.bak '{}i\\fpath=(~/.zsh/completions $fpath)' ~/.zshrc",
                            insert_line
                        ));
                        Output::command_example("rm -f ~/.zcompdump && exec zsh");
                    }
                } else {
                    Output::sub_item("Run these commands to complete setup:");
                    println!();

                    if needs_fpath {
                        Output::command_example(
                            r#"echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc"#,
                        );
                    }
                    if needs_compinit {
                        Output::command_example(
                            r#"echo 'autoload -Uz compinit && compinit' >> ~/.zshrc"#,
                        );
                    }
                    Output::command_example("source ~/.zshrc");
                }
            } else {
                Output::success("Zsh is properly configured for completions!");

                if using_omz {
                    println!();
                    Output::tip("If completions aren't working, clear Oh-My-Zsh cache:");
                    Output::command_example("rm -f ~/.zcompdump && exec zsh");
                }
            }
        }
    }

    println!();
    Output::section("Manual installation");
    Output::command_example("ca completions generate bash > ~/.bash_completion.d/ca");
    Output::command_example("ca completions generate zsh > ~/.zsh/completions/_ca");
    Output::command_example("ca completions generate fish > ~/.config/fish/completions/ca.fish");

    Ok(())
}

/// Check if completion is installed for a shell
fn check_completion_installed(shell: Shell) -> bool {
    let home_dir = match dirs::home_dir() {
        Some(dir) => dir,
        None => return false,
    };

    let possible_paths = match shell {
        Shell::Bash => vec![
            home_dir.join(".bash_completion.d/ca"),
            PathBuf::from("/usr/local/etc/bash_completion.d/ca"),
            PathBuf::from("/etc/bash_completion.d/ca"),
        ],
        Shell::Zsh => vec![
            home_dir.join(".oh-my-zsh/completions/_ca"),
            home_dir.join(".zsh/completions/_ca"),
            PathBuf::from("/usr/local/share/zsh/site-functions/_ca"),
        ],
        Shell::Fish => vec![home_dir.join(".config/fish/completions/ca.fish")],
        _ => return false,
    };

    possible_paths.iter().any(|path| path.exists())
}

/// Generate custom completion logic for dynamic values
fn generate_custom_completion(shell: Shell) -> String {
    match shell {
        Shell::Bash => {
            r#"
# Custom completion for ca switch command
_ca_switch_completion() {
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local stacks=$(ca completion-helper stack-names 2>/dev/null)
    COMPREPLY=($(compgen -W "$stacks" -- "$cur"))
}

# Replace the default completion for 'ca switch' with our custom function
complete -F _ca_switch_completion ca
"#.to_string()
        }
        Shell::Zsh => {
            r#"
# Custom completion for ca switch command
_ca_switch_completion() {
    local stacks=($(ca completion-helper stack-names 2>/dev/null))
    _describe 'stacks' stacks
}

# Override the switch completion
compdef _ca_switch_completion ca switch

# Explicitly bind the main completion function to 'ca'
# This ensures the completion works even if Oh-My-Zsh or other plugins interfere
compdef _ca ca
"#.to_string()
        }
        Shell::Fish => {
            r#"
# Custom completion for ca switch command
complete -c ca -f -n '__fish_seen_subcommand_from switch' -a '(ca completion-helper stack-names 2>/dev/null)'
"#.to_string()
        }
        _ => String::new(),
    }
}

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

    #[test]
    fn test_detect_shells() {
        let shells = detect_available_shells();
        assert!(!shells.is_empty());
    }

    #[test]
    fn test_generate_bash_completion() {
        let result = generate_completions(Shell::Bash);
        assert!(result.is_ok());
    }

    #[test]
    fn test_detect_current_shell() {
        // Test with a mocked SHELL environment variable
        std::env::set_var("SHELL", "/bin/zsh");
        let shell = detect_current_shell();
        assert_eq!(shell, Some(Shell::Zsh));

        std::env::set_var("SHELL", "/usr/bin/bash");
        let shell = detect_current_shell();
        assert_eq!(shell, Some(Shell::Bash));

        std::env::set_var("SHELL", "/usr/local/bin/fish");
        let shell = detect_current_shell();
        assert_eq!(shell, Some(Shell::Fish));

        std::env::set_var("SHELL", "/bin/unknown");
        let shell = detect_current_shell();
        assert_eq!(shell, None);

        // Clean up
        std::env::remove_var("SHELL");
    }
}