From f022574631bfb1b6a62f95d3259617f302059781 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Fri, 10 May 2024 13:47:16 +0300
Subject: [PATCH] feat: Support completion generating
Fixes: https://git.4rs.nl/awiteb/lprs/issues/20
---
src/cli/completion_command.rs | 37 +++++++++++++++++++++++++++++++++++
src/cli/mod.rs | 11 +++++++++--
2 files changed, 46 insertions(+), 2 deletions(-)
create mode 100644 src/cli/completion_command.rs
diff --git a/src/cli/completion_command.rs b/src/cli/completion_command.rs
new file mode 100644
index 0000000..e6e71e1
--- /dev/null
+++ b/src/cli/completion_command.rs
@@ -0,0 +1,37 @@
+// Lprs - A local CLI vault manager
+// Copyright (C) 2024 Awiteb
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see .
+
+use std::io;
+
+use clap::{Args, CommandFactory};
+
+use crate::{cli::Cli, vault::Vaults, LprsCommand, LprsResult};
+
+
+/// Generate shell auto completion
+#[derive(Debug, Args)]
+pub struct Completion {
+ /// Shell to generate completion for
+ shell: clap_complete::Shell,
+}
+
+
+impl LprsCommand for Completion {
+ fn run(self, _vault_manager: Vaults) -> LprsResult<()> {
+ clap_complete::generate(self.shell, &mut Cli::command(), "lprs", &mut io::stdout());
+ Ok(())
+ }
+}
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 7ce2e52..4206acf 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -25,6 +25,8 @@ use crate::{impl_commands, utils, vault::Vaults, LprsCommand, LprsResult};
pub mod add_command;
/// Clean command, used to clean the vaults file (remove all vaults)
pub mod clean_command;
+/// Generate shell completion
+pub mod completion_command;
/// Edit command, used to edit the vault content
pub mod edit_command;
/// Export command, used to export the vaults
@@ -65,9 +67,11 @@ pub enum Commands {
Export(export_command::Export),
/// Import vaults
Import(import_command::Import),
+ /// Generate shell completion
+ Completion(completion_command::Completion),
}
-impl_commands!(Commands, Add Remove List Clean Edit Gen Get Export Import);
+impl_commands!(Commands, Add Remove List Clean Edit Gen Get Export Import Completion);
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
@@ -116,7 +120,10 @@ impl Cli {
self.command.validate_args()?;
- let vault_manager = if matches!(self.command, Commands::Clean(..) | Commands::Gen(..)) {
+ let vault_manager = if matches!(
+ self.command,
+ Commands::Clean(..) | Commands::Gen(..) | Commands::Completion(..)
+ ) {
log::info!("Running command that don't need the vault manager");
// Returns empty vault manager for those commands don't need it
Vaults {