docker_tools/completion/
mod.rs1use clap::CommandFactory;
2use clap_complete::{Shell, generate};
3use std::io::{self, Write};
4
5pub fn run(shell: crate::cli::CompletionShell) -> i32 {
6 match shell {
7 crate::cli::CompletionShell::Bash => generate_script(Shell::Bash),
8 crate::cli::CompletionShell::Zsh => generate_script(Shell::Zsh),
9 }
10}
11
12fn generate_script(generator: Shell) -> i32 {
13 let mut command = crate::cli::Cli::command();
14 let bin_name = command.get_name().to_string();
15 if matches!(generator, Shell::Bash) {
16 let mut output = Vec::new();
17 generate(generator, &mut command, bin_name.clone(), &mut output);
18 let normalized = normalize_bash_completion(
19 String::from_utf8(output).expect("bash completion should be valid UTF-8"),
20 );
21 io::stdout()
22 .write_all(normalized.as_bytes())
23 .expect("failed to write bash completion");
24 return 0;
25 }
26
27 generate(generator, &mut command, bin_name, &mut io::stdout());
28 0
29}
30
31fn normalize_bash_completion(script: String) -> String {
32 script.replace("__subcmd__", "__")
33}