From e1f762ddc94f74e2b7c253250edbff1a1ab89bdc Mon Sep 17 00:00:00 2001 From: shimun Date: Thu, 3 Sep 2020 14:44:03 +0200 Subject: [PATCH] add subcommand to generate bash completions --- PKGBUILD | 2 ++ src/cli.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/PKGBUILD b/PKGBUILD index 9796d7b..c7ac4da 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -11,8 +11,10 @@ license=('MPL-2.0') build() { cargo build --release --locked --all-features --target-dir=target + ./target/release/fido2luks completions bash target } package() { install -Dm 755 target/release/${pkgname} -t "${pkgdir}/usr/bin" + install -Dm 644 target/fido2luks.bash "${pkgdir}/usr/share/bash-completion/completions/fido2luks" } diff --git a/src/cli.rs b/src/cli.rs index 52bab07..ecb844c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,7 @@ use crate::error::*; use crate::*; +use structopt::clap::{AppSettings, Shell}; use structopt::StructOpt; use ctap::{FidoCredential, FidoErrorKind}; @@ -303,6 +304,14 @@ pub enum Command { #[structopt(name = "connected")] Connected, Token(TokenCommand), + /// Generate bash completion scripts + #[structopt(name = "completions", setting = AppSettings::Hidden)] + GenerateCompletions { + /// Shell to generate completions for: bash, fish + #[structopt(possible_values = &["bash", "fish"])] + shell: String, + out_dir: PathBuf, + }, } ///LUKS2 token related operations @@ -725,5 +734,17 @@ pub fn run_cli() -> Fido2LuksResult<()> { Ok(()) } }, + Command::GenerateCompletions { shell, out_dir } => { + Args::clap().gen_completions( + env!("CARGO_PKG_NAME"), + match shell.as_ref() { + "bash" => Shell::Bash, + "fish" => Shell::Fish, + _ => unreachable!("structopt shouldn't allow us to reach this point"), + }, + &out_dir, + ); + Ok(()) + } } }