khelp 0.1.15

A tool for managing kubernetes contexts
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
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
use anyhow::{Context, Result};
use clap_complete::Shell;
use console::style;
use log::{debug, info};
use std::env;
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;

/// Generate shell completions
///
/// This function uses the clap_complete crate to generate shell completions for
/// the specified shell.
pub fn generate_completions(shell: Shell, install: bool) -> Result<()> {
    debug!(
        "Running completions command with shell: {:?}, install: {}",
        shell, install
    );

    if install {
        install_completions(shell)?;
    } else {
        // Generate a custom completion script based on the shell type
        // This completely avoids using clap_complete for stdout output
        match shell {
            Shell::Bash => {
                // Simple bash completions
                println!("# Bash completions for khelp");
                println!("_khelp_completions() {{");
                println!("  local cur prev");
                println!("  COMPREPLY=()");
                println!("  cur=\"${{COMP_WORDS[COMP_CWORD]}}\"");
                println!("  prev=\"${{COMP_WORDS[COMP_CWORD-1]}}\"");
                println!();
                println!("  if [ \"$COMP_CWORD\" -eq 1 ]; then");
                println!(
                    "    COMPREPLY=( $(compgen -W \"list current switch edit export delete rename add completions\" -- \"$cur\") )"
                );
                println!("    return 0");
                println!("  fi");
                println!();
                println!("  if [ \"$COMP_CWORD\" -ge 2 ]; then");
                println!("    case \"$prev\" in");
                println!("      switch|edit|export|delete|rename)");
                println!(
                    "        COMPREPLY=( $(compgen -W \"$(kubectl config get-contexts -o name 2>/dev/null)\" -- \"$cur\") )"
                );
                println!("        ;;");
                println!("      completions)");
                println!(
                    "        COMPREPLY=( $(compgen -W \"bash zsh fish powershell elvish\" -- \"$cur\") )"
                );
                println!("        ;;");
                println!("    esac");
                println!("  fi");
                println!("}}");
                println!();
                println!("complete -F _khelp_completions khelp");
            }
            Shell::Zsh => {
                // Simple zsh completions
                println!("#compdef khelp");
                println!();
                println!("_khelp() {{");
                println!("  local -a commands");
                println!("  commands=(");
                println!("    'list:List all available contexts'");
                println!("    'current:Get the current context'");
                println!("    'switch:Switch to a different context'");
                println!("    'edit:Edit a specific context'");
                println!("    'export:Export a specific context to stdout'");
                println!("    'delete:Delete a specific context'");
                println!("    'rename:Rename a context'");
                println!("    'add:Add contexts from an external kubeconfig file'");
                println!("    'completions:Generate shell completions'");
                println!("  )");
                println!();
                println!("  _arguments -C \\");
                println!("    '1: :->command' \\");
                println!("    '2: :->argument' \\");
                println!("    '*::arg:->args'");
                println!();
                println!("  case $state in");
                println!("    (command)");
                println!("      _describe -t commands 'khelp commands' commands");
                println!("      ;;");
                println!("    (argument)");
                println!("      case $line[1] in");
                println!("        (switch|edit|export|delete|rename)");
                println!("          local -a contexts");
                println!(
                    "          contexts=(${{{{(f)\"$(kubectl config get-contexts -o name 2>/dev/null)\"}}}}"
                );
                println!("          _describe 'contexts' contexts");
                println!("          ;;");
                println!("        (completions)");
                println!("          local -a shells");
                println!("          shells=('bash' 'zsh' 'fish' 'powershell' 'elvish')");
                println!("          _describe 'shells' shells");
                println!("          ;;");
                println!("      esac");
                println!("      ;;");
                println!("  esac");
                println!("}}");
                println!();
                println!("_khelp");
            }
            Shell::Fish => {
                // Simple fish completions
                println!("# Fish completions for khelp");
                println!();
                println!("function __khelp_get_contexts");
                println!("    kubectl config get-contexts -o name 2>/dev/null");
                println!("end");
                println!();
                println!("# Main commands");
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a list -d \"List all available contexts\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a current -d \"Get the current context\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a switch -d \"Switch to a different context\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a edit -d \"Edit a specific context\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a export -d \"Export a specific context to stdout\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a delete -d \"Delete a specific context\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a rename -d \"Rename a context\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a add -d \"Add contexts from an external kubeconfig file\""
                );
                println!(
                    "complete -c khelp -f -n \"not __fish_seen_subcommand_from list current switch edit export delete rename add completions\" -a completions -d \"Generate shell completions\""
                );
                println!();
                println!("# File path completion for add command");
                println!(
                    "complete -c khelp -F -n \"__fish_seen_subcommand_from add\" -d \"Kubeconfig file\""
                );
                println!();
                println!("# Context name completions");
                println!(
                    "complete -c khelp -f -n \"__fish_seen_subcommand_from switch\" -a \"(__khelp_get_contexts)\" -d \"Kubernetes context\""
                );
                println!(
                    "complete -c khelp -f -n \"__fish_seen_subcommand_from edit\" -a \"(__khelp_get_contexts)\" -d \"Kubernetes context\""
                );
                println!(
                    "complete -c khelp -f -n \"__fish_seen_subcommand_from export\" -a \"(__khelp_get_contexts)\" -d \"Kubernetes context\""
                );
                println!(
                    "complete -c khelp -f -n \"__fish_seen_subcommand_from delete\" -a \"(__khelp_get_contexts)\" -d \"Kubernetes context\""
                );
                println!(
                    "complete -c khelp -f -n \"__fish_seen_subcommand_from rename\" -a \"(__khelp_get_contexts)\" -d \"Kubernetes context\""
                );
                println!();
                println!("# Shell completions");
                println!(
                    "complete -c khelp -f -n \"__fish_seen_subcommand_from completions\" -a \"bash zsh fish powershell elvish\" -d \"Shell\""
                );
            }
            Shell::PowerShell => {
                // PowerShell completions
                println!("# PowerShell completions for khelp");
                println!("# Add this to your PowerShell profile ($PROFILE)");
                println!();
                println!("Register-ArgumentCompleter -Native -CommandName khelp -ScriptBlock {{");
                println!("    param($wordToComplete, $commandAst, $cursorPosition)");
                println!();
                println!("    $commands = @(");
                println!(
                    "        @{{ Name = 'list'; Description = 'List all available contexts' }}"
                );
                println!(
                    "        @{{ Name = 'current'; Description = 'Get the current context' }}"
                );
                println!(
                    "        @{{ Name = 'switch'; Description = 'Switch to a different context' }}"
                );
                println!("        @{{ Name = 'edit'; Description = 'Edit a specific context' }}");
                println!(
                    "        @{{ Name = 'export'; Description = 'Export a specific context to stdout' }}"
                );
                println!(
                    "        @{{ Name = 'delete'; Description = 'Delete a specific context' }}"
                );
                println!("        @{{ Name = 'rename'; Description = 'Rename a context' }}");
                println!(
                    "        @{{ Name = 'add'; Description = 'Add contexts from an external kubeconfig file' }}"
                );
                println!(
                    "        @{{ Name = 'completions'; Description = 'Generate shell completions' }}"
                );
                println!(
                    "        @{{ Name = 'update'; Description = 'Check for updates to khelp' }}"
                );
                println!("    )");
                println!();
                println!("    $elements = $commandAst.CommandElements");
                println!("    $command = $elements[1].Value");
                println!();
                println!("    # Complete subcommands");
                println!(
                    "    if ($elements.Count -eq 1 -or ($elements.Count -eq 2 -and $wordToComplete)) {{"
                );
                println!(
                    "        $commands | Where-Object {{ $_.Name -like \"$wordToComplete*\" }} | ForEach-Object {{"
                );
                println!(
                    "            [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Description)"
                );
                println!("        }}");
                println!("        return");
                println!("    }}");
                println!();
                println!("    # Complete context names for relevant commands");
                println!(
                    "    if ($command -in @('switch', 'edit', 'export', 'delete', 'rename')) {{"
                );
                println!("        $contexts = kubectl config get-contexts -o name 2>$null");
                println!("        if ($contexts) {{");
                println!(
                    "            $contexts | Where-Object {{ $_ -like \"$wordToComplete*\" }} | ForEach-Object {{"
                );
                println!(
                    "                [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', \"Kubernetes context\")"
                );
                println!("            }}");
                println!("        }}");
                println!("        return");
                println!("    }}");
                println!();
                println!("    # Complete shells for completions command");
                println!("    if ($command -eq 'completions') {{");
                println!(
                    "        @('bash', 'zsh', 'fish', 'powershell', 'elvish') | Where-Object {{ $_ -like \"$wordToComplete*\" }} | ForEach-Object {{"
                );
                println!(
                    "            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', \"Shell\")"
                );
                println!("        }}");
                println!("    }}");
                println!("}}");
            }
            _ => {
                println!("# Completions not supported for this shell");
                println!("# Supported shells: bash, zsh, fish, powershell");
            }
        }
    }

    // Ensure output is flushed
    io::stdout().flush().context("Failed to flush stdout")?;

    Ok(())
}

/// Detect the current shell
///
/// On Unix systems, this checks the $SHELL environment variable.
/// On Windows, this checks for PowerShell via PSModulePath environment variable.
pub fn detect_shell() -> Result<Shell> {
    // Try $SHELL first (Unix systems and some Windows terminals like Git Bash)
    if let Ok(shell_path) = env::var("SHELL") {
        let path = PathBuf::from(&shell_path);
        if let Some(shell_name) = path.file_name().and_then(|s| s.to_str()) {
            debug!("Detected shell from $SHELL: {}", shell_name);
            return match shell_name {
                "bash" => Ok(Shell::Bash),
                "zsh" => Ok(Shell::Zsh),
                "fish" => Ok(Shell::Fish),
                "pwsh" | "powershell" => Ok(Shell::PowerShell),
                _ => anyhow::bail!(
                    "Unsupported shell: {}. Please specify a supported shell (bash, zsh, fish, powershell)",
                    shell_name
                ),
            };
        }
    }

    // Windows: Check for PowerShell via PSModulePath environment variable
    if env::var("PSModulePath").is_ok() {
        debug!("Detected PowerShell via PSModulePath environment variable");
        return Ok(Shell::PowerShell);
    }

    // Windows: Check if running in cmd.exe
    if let Ok(comspec) = env::var("COMSPEC")
        && comspec.to_lowercase().contains("cmd.exe")
    {
        anyhow::bail!(
            "cmd.exe does not support tab completions. Please use PowerShell instead, or specify a shell explicitly."
        );
    }

    anyhow::bail!(
        "Could not detect shell. Please specify a shell explicitly (bash, zsh, fish, powershell)"
    )
}

/// Install completions for the specified shell
fn install_completions(shell: Shell) -> Result<()> {
    debug!(
        "Starting installation process for {:?} shell completions",
        shell
    );

    let shell = if shell == Shell::Bash
        || shell == Shell::Zsh
        || shell == Shell::Fish
        || shell == Shell::PowerShell
    {
        debug!("Shell {:?} is directly supported", shell);
        shell
    } else {
        // Auto-detect shell if not one of the supported ones
        debug!(
            "Shell {:?} support is limited. Attempting to detect current shell...",
            shell
        );
        let detected = detect_shell()?;
        debug!("Detected shell: {:?}", detected);
        detected
    };

    debug!("Installing completions for shell: {:?}", shell);

    let result = match shell {
        Shell::Bash => {
            debug!("Installing Bash completions");
            install_bash_completions()
        }
        Shell::Zsh => {
            debug!("Installing Zsh completions");
            install_zsh_completions()
        }
        Shell::Fish => {
            debug!("Installing Fish completions");
            install_fish_completions()
        }
        Shell::PowerShell => {
            debug!("Installing PowerShell completions");
            install_powershell_completions()
        }
        _ => {
            debug!("Unsupported shell: {:?}", shell);
            anyhow::bail!("Completions installation not implemented for {:?}", shell)
        }
    };

    debug!("Installation process result: {:?}", result.is_ok());
    result
}

/// Install Bash completions
fn install_bash_completions() -> Result<()> {
    info!("Installing Bash completions for khelp...");

    let home = dirs::home_dir().context("Could not find home directory")?;
    let completions_dir = home.join(".bash_completion.d");

    debug!(
        "Creating completions directory: {}",
        completions_dir.display()
    );

    // Create completions directory if it doesn't exist
    fs::create_dir_all(&completions_dir).context("Failed to create completions directory")?;

    // Generate the completion script content
    let content = r#"#!/usr/bin/env bash

# Dynamic Kubernetes context completion for khelp in Bash

# Get the Kubernetes contexts from kubectl
_khelp_get_contexts() {
    kubectl config get-contexts -o name 2>/dev/null
}

# Complete khelp commands and options
_khelp_complete() {
    local cur prev words cword
    _init_completion || return

    # Complete first argument (command)
    if [[ $cword -eq 1 ]]; then
        COMPREPLY=($(compgen -W "list current switch edit export delete rename add completions" -- "$cur"))
        return 0
    fi

    # Complete second argument based on first argument
    if [[ $cword -eq 2 ]]; then
        case "$prev" in
            switch|edit|export|delete|rename)
                # Complete with context names
                COMPREPLY=($(compgen -W "$(_khelp_get_contexts)" -- "$cur"))
                return 0
                ;;
            completions)
                # Complete with shell names
                COMPREPLY=($(compgen -W "bash zsh fish powershell elvish" -- "$cur"))
                return 0
                ;;
            *)
                return 0
                ;;
        esac
    fi

    return 0
}

# Register the completion function
complete -F _khelp_complete khelp
"#;

    // Write the completion script
    let completions_file = completions_dir.join("khelp");
    fs::write(&completions_file, content).context("Failed to write completion script")?;

    // Make the script executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&completions_file)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&completions_file, perms)?;
    }

    // Update .bashrc if needed
    let bashrc_path = home.join(".bashrc");
    if let Ok(bashrc_content) = fs::read_to_string(&bashrc_path) {
        let source_line = format!("source {}", completions_file.display());
        if !bashrc_content.contains(&source_line) {
            let mut bashrc_file = fs::OpenOptions::new()
                .append(true)
                .open(bashrc_path)
                .context("Failed to open .bashrc")?;

            writeln!(bashrc_file, "\n# Source khelp completions")?;
            writeln!(bashrc_file, "{}", source_line)?;
            debug!("Added source line to ~/.bashrc");
        }
    }

    println!(
        "{}",
        style("Bash completions installed successfully!")
            .green()
            .bold()
    );
    println!(
        "Please run 'source ~/.bash_completion.d/khelp' to enable completions in your current session."
    );

    Ok(())
}

/// Install Zsh completions
fn install_zsh_completions() -> Result<()> {
    info!("Installing Zsh completions for khelp...");

    let home = dirs::home_dir().context("Could not find home directory")?;
    let completions_dir = home.join(".zfunc");

    // Create completions directory if it doesn't exist
    fs::create_dir_all(&completions_dir).context("Failed to create completions directory")?;

    // Generate the completion script content
    let content = r#"#compdef khelp

# Dynamic Kubernetes context completion for khelp in Zsh

# Function to get Kubernetes contexts
_khelp_get_contexts() {
    local -a contexts
    contexts=(${(f)"$(kubectl config get-contexts -o name 2>/dev/null)"})
    _describe 'contexts' contexts
}

# Define the completion function
_khelp() {
    local line state

    _arguments -C \
        '1: :->command' \
        '2: :->argument' \
        '*: :->args'

    case $state in
        command)
            _values "command" \
                "list[List all available contexts]" \
                "current[Get the current context]" \
                "switch[Switch to a different context]" \
                "edit[Edit a specific context]" \
                "export[Export a specific context to stdout]" \
                "delete[Delete a specific context]" \
                "rename[Rename a context]" \
                "add[Add contexts from an external kubeconfig file]" \
                "completions[Generate shell completions]"
            ;;
        argument)
            case $line[1] in
                switch|edit|export|delete|rename)
                    _khelp_get_contexts
                    ;;
                completions)
                    _values "shell" "bash" "zsh" "fish" "powershell" "elvish"
                    ;;
            esac
            ;;
    esac
}

# Register the completion function
compdef _khelp khelp
"#;

    // Write the completion script
    let completions_file = completions_dir.join("_khelp");
    fs::write(&completions_file, content).context("Failed to write completion script")?;

    // Make the script executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&completions_file)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&completions_file, perms)?;
    }

    // Update .zshrc if needed
    let zshrc_path = home.join(".zshrc");
    if let Ok(zshrc_content) = fs::read_to_string(&zshrc_path)
        && !zshrc_content.contains("fpath=(~/.zfunc")
    {
        let mut zshrc_file = fs::OpenOptions::new()
            .append(true)
            .open(zshrc_path)
            .context("Failed to open .zshrc")?;

        writeln!(zshrc_file, "\n# Add khelp completions to fpath")?;
        writeln!(zshrc_file, "fpath=(~/.zfunc $fpath)")?;
        writeln!(zshrc_file, "autoload -Uz compinit && compinit")?;
        debug!("Added fpath configuration to ~/.zshrc");
    }

    println!(
        "{}",
        style("Zsh completions installed successfully!")
            .green()
            .bold()
    );
    println!("Please run 'source ~/.zshrc' to enable completions in your current session.");

    Ok(())
}

/// Install Fish completions
fn install_fish_completions() -> Result<()> {
    info!("Installing Fish completions for khelp...");

    let home = dirs::home_dir().context("Could not find home directory")?;
    debug!("Home directory: {}", home.display());

    let completions_dir = home.join(".config/fish/completions");
    debug!("Fish completions directory: {}", completions_dir.display());

    // Create completions directory if it doesn't exist
    debug!("Creating fish completions directory...");
    fs::create_dir_all(&completions_dir).context("Failed to create fish completions directory")?;
    debug!("Directory created successfully");

    // Generate the completion script content
    debug!("Preparing fish completion script...");
    let content = r#"# Dynamic Kubernetes context completion for khelp in Fish

function __khelp_get_contexts
    kubectl config get-contexts -o name 2>/dev/null
end

# Define command completions
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a list -d "List all available contexts"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a current -d "Get the current context"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a switch -d "Switch to a different context"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a edit -d "Edit a specific context"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a export -d "Export a specific context to stdout"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a delete -d "Delete a specific context"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a rename -d "Rename a context"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a add -d "Add contexts from an external kubeconfig file"
complete -c khelp -f -n "not __fish_seen_subcommand_from list current switch edit export delete rename add completions" -a completions -d "Generate shell completions"

# File path completion for add command
complete -c khelp -F -n "__fish_seen_subcommand_from add" -d "Kubeconfig file"

# Define context name completions for the relevant commands
complete -c khelp -f -n "__fish_seen_subcommand_from switch" -a "(__khelp_get_contexts)" -d "Kubernetes context"
complete -c khelp -f -n "__fish_seen_subcommand_from edit" -a "(__khelp_get_contexts)" -d "Kubernetes context"
complete -c khelp -f -n "__fish_seen_subcommand_from export" -a "(__khelp_get_contexts)" -d "Kubernetes context"
complete -c khelp -f -n "__fish_seen_subcommand_from delete" -a "(__khelp_get_contexts)" -d "Kubernetes context"
complete -c khelp -f -n "__fish_seen_subcommand_from rename" -a "(__khelp_get_contexts)" -d "Kubernetes context"

# Define shell completions for the completions command
complete -c khelp -f -n "__fish_seen_subcommand_from completions" -a "bash zsh fish powershell elvish" -d "Shell"
"#;

    // Write the completion script
    let completions_file = completions_dir.join("khelp.fish");
    debug!(
        "Writing completion script to: {}",
        completions_file.display()
    );
    fs::write(&completions_file, content).context("Failed to write fish completion script")?;
    debug!("Fish completion script written successfully");

    println!(
        "{}",
        style("Fish completions installed successfully!")
            .green()
            .bold()
    );
    println!("Fish will automatically load the completions for new sessions.");

    Ok(())
}

/// Install PowerShell completions
fn install_powershell_completions() -> Result<()> {
    info!("Installing PowerShell completions for khelp...");

    // Determine the PowerShell profile path based on platform
    let profile_dir = if cfg!(target_os = "windows") {
        // Windows: Use Documents\PowerShell for PowerShell 7+ or Documents\WindowsPowerShell for 5.x
        dirs::document_dir()
            .context("Could not find Documents directory")?
            .join("PowerShell")
    } else {
        // Unix: PowerShell Core uses ~/.config/powershell
        dirs::config_dir()
            .context("Could not find config directory")?
            .join("powershell")
    };

    debug!("PowerShell profile directory: {}", profile_dir.display());

    // Create the profile directory if it doesn't exist
    fs::create_dir_all(&profile_dir).context("Failed to create PowerShell profile directory")?;

    // Generate the completion script content
    let content = r#"# khelp PowerShell completions
# Generated by khelp completions --install

Register-ArgumentCompleter -Native -CommandName khelp -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $commands = @(
        @{ Name = 'list'; Description = 'List all available contexts' }
        @{ Name = 'current'; Description = 'Get the current context' }
        @{ Name = 'switch'; Description = 'Switch to a different context' }
        @{ Name = 'edit'; Description = 'Edit a specific context' }
        @{ Name = 'export'; Description = 'Export a specific context to stdout' }
        @{ Name = 'delete'; Description = 'Delete a specific context' }
        @{ Name = 'rename'; Description = 'Rename a context' }
        @{ Name = 'add'; Description = 'Add contexts from an external kubeconfig file' }
        @{ Name = 'completions'; Description = 'Generate shell completions' }
        @{ Name = 'update'; Description = 'Check for updates to khelp' }
    )

    $elements = $commandAst.CommandElements
    $command = if ($elements.Count -gt 1) { $elements[1].Value } else { $null }

    # Complete subcommands
    if ($elements.Count -eq 1 -or ($elements.Count -eq 2 -and $wordToComplete)) {
        $commands | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, 'ParameterValue', $_.Description)
        }
        return
    }

    # Complete context names for relevant commands
    if ($command -in @('switch', 'edit', 'export', 'delete', 'rename')) {
        $contexts = kubectl config get-contexts -o name 2>$null
        if ($contexts) {
            $contexts | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
                [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', "Kubernetes context")
            }
        }
        return
    }

    # Complete shells for completions command
    if ($command -eq 'completions') {
        @('bash', 'zsh', 'fish', 'powershell', 'elvish') | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', "Shell")
        }
    }
}
"#;

    // Write the completion script to a separate file
    let completions_file = profile_dir.join("khelp_completions.ps1");
    debug!(
        "Writing completion script to: {}",
        completions_file.display()
    );
    fs::write(&completions_file, content)
        .context("Failed to write PowerShell completion script")?;

    // Update the PowerShell profile to source the completions
    let profile_path = profile_dir.join("Microsoft.PowerShell_profile.ps1");
    let source_line = format!(". \"{}\"", completions_file.display());

    // Check if the profile exists and if it already sources our completions
    let should_update = if let Ok(profile_content) = fs::read_to_string(&profile_path) {
        !profile_content.contains("khelp_completions.ps1")
    } else {
        true
    };

    if should_update {
        let mut profile_file = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&profile_path)
            .context("Failed to open PowerShell profile")?;

        writeln!(profile_file, "\n# khelp completions")?;
        writeln!(profile_file, "{}", source_line)?;
        debug!("Added source line to PowerShell profile");
    }

    println!(
        "{}",
        style("PowerShell completions installed successfully!")
            .green()
            .bold()
    );
    println!("Completions will be loaded automatically in new PowerShell sessions.");
    println!(
        "To enable in current session, run: . \"{}\"",
        completions_file.display()
    );

    Ok(())
}