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.
This commit is contained in:
Matej Janezic 2023-08-09 14:45:07 +02:00
parent e061d6dc6c
commit 2754a3bb30
Signed by: janezicmatej
SSH Key Fingerprint: SHA256:3vE2ixAKI8uRNhob5xDwoa9mMPUsxsM4QRHNcPuMHew
4 changed files with 28 additions and 17 deletions

View File

@ -7,6 +7,7 @@ edition = "2021"
license = "MIT" license = "MIT"
authors = ["Matej Janežič <janezic.mj@gmail.com>"] authors = ["Matej Janežič <janezic.mj@gmail.com>"]
repository = "https://github.com/janezicmatej/ahab.git" 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 # 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" clap_complete = "4.3.0"
anyhow = "1.0.71" anyhow = "1.0.71"
dotenvy = "0.15.7" dotenvy = "0.15.7"
[build-dependencies]
clap = { version = "4.3.0", features = ["derive"] }
clap_complete = "4.3.0"

22
build.rs Normal file
View File

@ -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(())
}

View File

@ -1,6 +1,5 @@
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)]
@ -12,12 +11,6 @@ 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
Docker { Docker {
#[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::{CommandFactory, Parser}; use clap::Parser;
fn main() -> Result<()> { fn main() -> Result<()> {
// always load dotenv on start // always load dotenv on start
@ -13,15 +13,6 @@ 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::Docker { command } => match command { cli::Commands::Docker { command } => match command {
cli::Docker::StopAll => scripts::docker::stop_all(), cli::Docker::StopAll => scripts::docker::stop_all(),
}, },