From 2754a3bb309011d7e64c2f4b2c438391302943eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Wed, 9 Aug 2023 14:45:07 +0200 Subject: [PATCH] feat: compile time completion generation Removed `completion` command in favor of compile time completion generation. This tool will only be build from source for the foreseeable future so this makes sense for now. --- Cargo.toml | 5 +++++ build.rs | 22 ++++++++++++++++++++++ src/cli/ahab.rs | 7 ------- src/main.rs | 11 +---------- 4 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index cfb96a6..756a73b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" license = "MIT" authors = ["Matej Janežič "] repository = "https://github.com/janezicmatej/ahab.git" +build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -15,3 +16,7 @@ clap = { version = "4.3.0", features = ["derive"] } clap_complete = "4.3.0" anyhow = "1.0.71" dotenvy = "0.15.7" + +[build-dependencies] +clap = { version = "4.3.0", features = ["derive"] } +clap_complete = "4.3.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..cb4c17a --- /dev/null +++ b/build.rs @@ -0,0 +1,22 @@ +use clap::{CommandFactory, ValueEnum}; +use clap_complete::{generate_to, Shell}; +use std::{env, io::Error}; + +include!("src/cli/mod.rs"); + +fn main() -> Result<(), Error> { + let outdir = match env::var_os("OUT_DIR") { + None => return Ok(()), + Some(outdir) => outdir, + }; + + let mut cmd = ahab::Ahab::command(); + + for shell in Shell::value_variants() { + let path = generate_to(*shell, &mut cmd, "ahab", &outdir)?; + + println!("cargo:warning=completion file is generated: {path:?}"); + } + + Ok(()) +} diff --git a/src/cli/ahab.rs b/src/cli/ahab.rs index 3979097..5b0ef07 100644 --- a/src/cli/ahab.rs +++ b/src/cli/ahab.rs @@ -1,6 +1,5 @@ use super::{Django, Docker, DockerCompose, Postgres}; use clap::{Parser, Subcommand}; -use clap_complete::Shell; /// A program for interacting with various dockerized applications. #[derive(Parser, Debug)] @@ -12,12 +11,6 @@ pub struct Ahab { #[derive(Debug, Subcommand)] pub enum Commands { - /// Generate completion files - Completion { - #[arg(value_enum)] - shell: Shell, - }, - /// Docker related subcommands Docker { #[command(subcommand)] diff --git a/src/main.rs b/src/main.rs index 5291957..cd22ba9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use ahab::{cli, scripts}; use anyhow::Result; -use clap::{CommandFactory, Parser}; +use clap::Parser; fn main() -> Result<()> { // always load dotenv on start @@ -13,15 +13,6 @@ fn main() -> Result<()> { let args = cli::Ahab::parse(); match args.command { - cli::Commands::Completion { shell } => { - clap_complete::generate( - shell, - &mut cli::Ahab::command(), - "ahab", - &mut std::io::stdout(), - ); - Ok(()) - } cli::Commands::Docker { command } => match command { cli::Docker::StopAll => scripts::docker::stop_all(), },