Skip to content

Commit

Permalink
feat: add mangen and completions binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed May 18, 2024
1 parent 50e23cb commit e68a7a3
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/bin/completions.rs
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
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(())
}
30 changes: 30 additions & 0 deletions src/bin/mangen.rs
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
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::<u8>::new();
man.render(&mut buffer)?;
fs::write(&out_path, buffer)?;
println!("Man page is generated at {out_path:?}");
Ok(())
}

0 comments on commit e68a7a3

Please sign in to comment.