bootstrap: recreate completion function to use

clap_completion::generator::Generator trait to specify generic parameter
This commit is contained in:
Nathan Fisher 2022-12-22 19:09:44 -05:00
parent 0cc2bd507b
commit 480960ce2b

View File

@ -1,8 +1,13 @@
use crate::cmd;
use clap::{value_parser, Arg, ArgMatches, Command, ArgAction};
use clap_complete::{generate_to, shells};
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use clap_complete::{generate_to, shells, Generator};
use clap_complete_nushell::Nushell;
use clap_mangen::Man;
use std::{fs, io, path::{Path, PathBuf}, process};
use std::{
fs, io,
path::{Path, PathBuf},
process,
};
const COMMANDS: [fn() -> clap::Command; 8] = [
cmd::bootstrap::cli,
@ -109,26 +114,22 @@ fn manpage(prefix: &str, f: &dyn Fn() -> Command) -> Result<(), io::Error> {
fn manpages(prefix: &str) -> Result<(), io::Error> {
println!("Generating Unix man pages:");
COMMANDS.iter().try_for_each(|cmd| {
manpage(prefix, &cmd)
})?;
COMMANDS.iter().try_for_each(|cmd| manpage(prefix, &cmd))?;
Ok(())
}
fn gen_bash_completion(outdir: &Path, f: &dyn Fn() -> Command) -> Result<(), io::Error> {
fn generate_completions(
outdir: &Path,
f: &dyn Fn() -> Command,
gen: impl Generator,
) -> Result<(), io::Error> {
let cmd = f();
let name = cmd.get_name();
let mut cmd = cmd.clone();
let path = generate_to(shells::Bash, &mut cmd, name, outdir)?;
println!(" {}", path.display());
Ok(())
}
fn gen_fish_completion(outdir: &Path, f: &dyn Fn() -> Command) -> Result<(), io::Error> {
let cmd = f();
let name = cmd.get_name();
let mut cmd = cmd.clone();
let path = generate_to(shells::Fish, &mut cmd, name, outdir)?;
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
let path = generate_to(gen, &mut cmd, name, outdir)?;
println!(" {}", path.display());
Ok(())
}
@ -139,25 +140,33 @@ fn completions(prefix: &str, matches: &ArgMatches) -> Result<(), io::Error> {
let outdir: PathBuf = [prefix, "share", "bash-completion", "completion"]
.iter()
.collect();
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
COMMANDS.iter().try_for_each(|cmd| gen_bash_completion(&outdir, &cmd) )?;
COMMANDS
.iter()
.try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Bash))?;
}
if matches.get_flag("fish") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "fish", "completions"]
let outdir: PathBuf = [prefix, "share", "fish", "completions"].iter().collect();
COMMANDS
.iter()
.collect();
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
COMMANDS.iter().try_for_each(|cmd| gen_fish_completion(&outdir, &cmd) )?;
.try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Fish))?;
}
if matches.get_flag("nu") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "nu", "completions"].iter().collect();
COMMANDS
.iter()
.try_for_each(|cmd| generate_completions(&outdir, &cmd, Nushell))?;
}
if matches.get_flag("pwsh") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "pwsh", "completions"].iter().collect();
COMMANDS
.iter()
.try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::PowerShell))?;
}
if matches.get_flag("zsh") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "zsh", "site-functions"].iter().collect();
COMMANDS
.iter()
.try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Zsh))?;
}
Ok(())
}