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(name = "completions", about = "Generate shell completions")]
11pub struct CompletionsCommand {
12    /// The shell to generate completions for.
13    #[arg(value_enum)]
14    pub shell: Shell,
15}
16
17/// Generate shell completions and print to stdout.
18///
19/// # Usage
20///
21/// ```bash
22/// # Bash
23/// auths completions bash > ~/.local/share/bash-completion/completions/auths
24///
25/// # Zsh
26/// auths completions zsh > ~/.zfunc/_auths
27///
28/// # Fish
29/// auths completions fish > ~/.config/fish/completions/auths.fish
30///
31/// # PowerShell
32/// auths completions powershell > auths.ps1
33/// ```
34pub fn handle_completions<C: CommandFactory>(cmd: CompletionsCommand) -> Result<()> {
35    let mut command = C::command();
36    let name = command.get_name().to_string();
37    generate(cmd.shell, &mut command, name, &mut io::stdout());
38    Ok(())
39}
40
41impl crate::commands::executable::ExecutableCommand for CompletionsCommand {
42    fn execute(&self, _ctx: &crate::config::CliConfig) -> anyhow::Result<()> {
43        handle_completions::<crate::cli::AuthsCli>(self.clone())
44    }
45}