From e68a7a3a2800a2b061a35db5a1f8f45caf473686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sat, 18 May 2024 14:46:03 +0300 Subject: [PATCH] feat: add mangen and completions binaries --- Cargo.lock | 27 +++++++++++++++++++++++++++ Cargo.toml | 11 +++++++++++ src/bin/completions.rs | 26 ++++++++++++++++++++++++++ src/bin/mangen.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/bin/completions.rs create mode 100644 src/bin/mangen.rs diff --git a/Cargo.lock b/Cargo.lock index 74be312..3ffa407 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,6 +258,15 @@ dependencies = [ "terminal_size 0.3.0", ] +[[package]] +name = "clap_complete" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd79504325bf38b10165b02e89b4347300f855f273c4cb30c4a3209e6583275e" +dependencies = [ + "clap 4.5.4", +] + [[package]] name = "clap_derive" version = "4.5.4" @@ -276,6 +285,16 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +[[package]] +name = "clap_mangen" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1dd95b5ebb5c1c54581dd6346f3ed6a79a3eef95dd372fc2ac13d535535300e" +dependencies = [ + "clap 4.5.4", + "roff", +] + [[package]] name = "colorchoice" version = "1.0.1" @@ -490,6 +509,8 @@ name = "flawz" version = "0.1.0-rc.0" dependencies = [ "clap 4.5.4", + "clap_complete", + "clap_mangen", "crossterm", "nvd_cve", "ratatui", @@ -1263,6 +1284,12 @@ dependencies = [ "winreg", ] +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "rusqlite" version = "0.31.0" diff --git a/Cargo.toml b/Cargo.toml index eace9b1..99100fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,15 @@ keywords = ["cve", "nvd", "tui", "flaw", "security", "vulnerability"] categories = ["command-line-utilities"] edition = "2021" rust-version = "1.74.1" +default-run = "flawz" + +[[bin]] +name = "flawz-completions" +path = "src/bin/completions.rs" + +[[bin]] +name = "flawz-mangen" +path = "src/bin/mangen.rs" [dependencies] nvd_cve = "0.2.0" @@ -21,6 +30,8 @@ textwrap = "0.16.1" tui-input = "0.8.0" tui-popup = "0.3.1" clap = { version = "4.5.4", features = ["derive", "env", "wrap_help", "cargo"] } +clap_complete = "4.5.2" +clap_mangen = "0.2.20" [profile.dev] opt-level = 0 diff --git a/src/bin/completions.rs b/src/bin/completions.rs new file mode 100644 index 0000000..0bdccfd --- /dev/null +++ b/src/bin/completions.rs @@ -0,0 +1,26 @@ +use clap::{CommandFactory, ValueEnum}; +use clap_complete::Shell; +use flawz::args::Args; +use std::env; +use std::io::Result; + +/// Environment variable for the output directory. +const OUT_DIR_ENV: &str = "OUT_DIR"; + +/// Shell completions can be created with: +/// +/// ```sh +/// cargo run --bin flawz-completions +/// ``` +/// +/// in a directory specified by the environment variable OUT_DIR. +/// See +fn main() -> Result<()> { + let out_dir = env::var(OUT_DIR_ENV).unwrap_or_else(|_| panic!("{OUT_DIR_ENV} is not set")); + let mut app = Args::command(); + for &shell in Shell::value_variants() { + clap_complete::generate_to(shell, &mut app, env!("CARGO_PKG_NAME"), &out_dir)?; + } + println!("Completion scripts are generated in {out_dir:?}"); + Ok(()) +} diff --git a/src/bin/mangen.rs b/src/bin/mangen.rs new file mode 100644 index 0000000..5adc935 --- /dev/null +++ b/src/bin/mangen.rs @@ -0,0 +1,30 @@ +use clap::CommandFactory; +use clap_mangen::Man; +use flawz::args::Args; +use std::env; +use std::fs; +use std::io::Result; +use std::path::PathBuf; + +/// Environment variable for the output directory. +const OUT_DIR_ENV: &str = "OUT_DIR"; + +/// Man page can be created with: +/// +/// ```sh +/// cargo run --bin flawz-mangen +/// ```` +/// +/// in a directory specified by the environment variable OUT_DIR. +/// See +fn main() -> Result<()> { + let out_dir = env::var(OUT_DIR_ENV).unwrap_or_else(|_| panic!("{OUT_DIR_ENV} is not set")); + let out_path = PathBuf::from(out_dir).join(format!("{}.1", env!("CARGO_PKG_NAME"))); + let app = Args::command(); + let man = Man::new(app); + let mut buffer = Vec::::new(); + man.render(&mut buffer)?; + fs::write(&out_path, buffer)?; + println!("Man page is generated at {out_path:?}"); + Ok(()) +}