Skip to main content

auths_cli/commands/
completions.rs

1//! Shell completion generation.
2
3use anyhow::Result;
4use clap::{CommandFactory, Parser};
5use clap_complete::{Shell, generate};
6use std::io;
7
8/// Generate shell completions for auths.
9#[derive(Parser, Debug, Clone)]
10#[command(
11    name = "completions",
12    about = "Generate shell completions",
13    after_help = "Examples:
14  auths completions bash              # Print Bash completions
15  auths completions zsh               # Print Zsh completions
16  auths completions fish              # Print Fish completions
17  auths completions powershell        # Print PowerShell completions
18
19Installation:
20  Bash:      auths completions bash > ~/.local/share/bash-completion/completions/auths
21  Zsh:       auths completions zsh > ~/.zfunc/_auths
22  Fish:      auths completions fish > ~/.config/fish/completions/auths.fish
23  PowerShell: auths completions powershell > auths.ps1
24
25After installation, restart your shell or source the completion file.
26
27Related:
28  auths --help       — Show all commands
29  auths <command> -h — Show command help"
30)]
31pub struct CompletionsCommand {
32    /// The shell to generate completions for.
33    #[arg(value_enum)]
34    pub shell: Shell,
35}
36
37/// Generate shell completions and print to stdout.
38///
39/// # Usage
40///
41/// ```bash
42/// # Bash
43/// auths completions bash > ~/.local/share/bash-completion/completions/auths
44///
45/// # Zsh
46/// auths completions zsh > ~/.zfunc/_auths
47///
48/// # Fish
49/// auths completions fish > ~/.config/fish/completions/auths.fish
50///
51/// # PowerShell
52/// auths completions powershell > auths.ps1
53/// ```
54pub fn handle_completions<C: CommandFactory>(cmd: CompletionsCommand) -> Result<()> {
55    let mut command = C::command();
56    let name = command.get_name().to_string();
57    generate(cmd.shell, &mut command, name, &mut io::stdout());
58    Ok(())
59}
60
61impl crate::commands::executable::ExecutableCommand for CompletionsCommand {
62    fn execute(&self, _ctx: &crate::config::CliConfig) -> anyhow::Result<()> {
63        handle_completions::<crate::cli::AuthsCli>(self.clone())
64    }
65}