rustic_rs/commands/
completions.rs

1//! `completions` subcommand
2
3use abscissa_core::{Command, Runnable};
4
5use std::io::Write;
6
7use clap::CommandFactory;
8
9use clap_complete::{Generator, generate, shells};
10
11/// `completions` subcommand
12#[derive(clap::Parser, Command, Debug)]
13pub(crate) struct CompletionsCmd {
14    /// Shell to generate completions for
15    #[clap(value_enum)]
16    sh: Variant,
17}
18
19#[derive(Clone, Debug, clap::ValueEnum)]
20pub(super) enum Variant {
21    Bash,
22    Fish,
23    Zsh,
24    Powershell,
25}
26
27impl Runnable for CompletionsCmd {
28    fn run(&self) {
29        match self.sh {
30            Variant::Bash => generate_completion(shells::Bash, &mut std::io::stdout()),
31            Variant::Fish => generate_completion(shells::Fish, &mut std::io::stdout()),
32            Variant::Zsh => generate_completion(shells::Zsh, &mut std::io::stdout()),
33            Variant::Powershell => generate_completion(shells::PowerShell, &mut std::io::stdout()),
34        }
35    }
36}
37
38pub fn generate_completion<G: Generator>(shell: G, buf: &mut dyn Write) {
39    let mut command = crate::commands::EntryPoint::command();
40    generate(
41        shell,
42        &mut command,
43        option_env!("CARGO_BIN_NAME").unwrap_or("rustic"),
44        buf,
45    );
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_completions() {
54        generate_completion(shells::Bash, &mut std::io::sink());
55        generate_completion(shells::Fish, &mut std::io::sink());
56        generate_completion(shells::PowerShell, &mut std::io::sink());
57        generate_completion(shells::Zsh, &mut std::io::sink());
58    }
59}