feat: add clap_complete for generating completion files

`ahab generate <shell>` can now be used to generte completion files for
this tool.
This commit is contained in:
Matej Janezic 2023-05-27 13:26:35 +02:00
parent a9a5e401dc
commit 61e7556997
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
4 changed files with 28 additions and 1 deletions

10
Cargo.lock generated
View File

@ -8,6 +8,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"clap_complete",
"dotenvy", "dotenvy",
] ]
@ -102,6 +103,15 @@ dependencies = [
"strsim", "strsim",
] ]
[[package]]
name = "clap_complete"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a04ddfaacc3bc9e6ea67d024575fafc2a813027cf374b8f24f7bc233c6b6be12"
dependencies = [
"clap",
]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.3.0" version = "4.3.0"

View File

@ -12,5 +12,6 @@ repository = "https://github.com/janezicmatej/ahab.git"
[dependencies] [dependencies]
clap = { version = "4.3.0", features = ["derive"] } clap = { version = "4.3.0", features = ["derive"] }
clap_complete = "4.3.0"
anyhow = "1.0.71" anyhow = "1.0.71"
dotenvy = "0.15.7" dotenvy = "0.15.7"

View File

@ -1,5 +1,6 @@
use super::{Django, Docker, DockerCompose, Postgres}; use super::{Django, Docker, DockerCompose, Postgres};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use clap_complete::Shell;
/// A program for interacting with various dockerized applications. /// A program for interacting with various dockerized applications.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -11,6 +12,12 @@ pub struct Ahab {
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum Commands { pub enum Commands {
/// Generate completion files
Completion {
#[arg(value_enum)]
shell: Shell,
},
/// Docker related subcommands /// Docker related subcommands
D { D {
#[command(subcommand)] #[command(subcommand)]

View File

@ -1,7 +1,7 @@
use ahab::{cli, scripts}; use ahab::{cli, scripts};
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::{CommandFactory, Parser};
fn main() -> Result<()> { fn main() -> Result<()> {
// always load dotenv on start // always load dotenv on start
@ -13,6 +13,15 @@ fn main() -> Result<()> {
let args = cli::Ahab::parse(); let args = cli::Ahab::parse();
match args.command { match args.command {
cli::Commands::Completion { shell } => {
clap_complete::generate(
shell,
&mut cli::Ahab::command(),
"ahab",
&mut std::io::stdout(),
);
Ok(())
}
cli::Commands::D { command } => match command { cli::Commands::D { command } => match command {
cli::Docker::StopAll => scripts::docker::stop_all(), cli::Docker::StopAll => scripts::docker::stop_all(),
}, },