From 07e705b8ee144ceb5b9d7d45f1ac13ec6d197ae4 Mon Sep 17 00:00:00 2001 From: Joseph LaFreniere Date: Mon, 15 Jan 2024 18:22:26 -0600 Subject: [PATCH] Generate manpage as part of build (#83) * Generate manpage via `clap_mangen` * Cleanup CLI docstrings --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 3 +++ build.rs | 21 +++++++++++++++++++++ src/args.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 44 ++------------------------------------------ 5 files changed, 84 insertions(+), 42 deletions(-) create mode 100644 build.rs create mode 100644 src/args.rs diff --git a/Cargo.lock b/Cargo.lock index e303aea..4fa8d45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,6 +189,16 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" +[[package]] +name = "clap_mangen" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3be86020147691e1d2ef58f75346a3d4d94807bfc473e377d52f09f0f7d77f7" +dependencies = [ + "clap 4.4.8", + "roff", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -711,6 +721,7 @@ name = "ripsecrets" version = "0.1.7" dependencies = [ "clap 4.4.8", + "clap_mangen", "criterion", "grep", "ignore", @@ -722,6 +733,12 @@ dependencies = [ "termcolor", ] +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "rustix" version = "0.38.25" diff --git a/Cargo.toml b/Cargo.toml index 9a133e8..8246292 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,6 @@ criterion = { version = "0.3", features = ["html_reports"] } name = "find_secrets" harness = false +[build-dependencies] +clap = { version = "4.2.5", features = ["derive"] } +clap_mangen = "0.2.15" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..fd0bb36 --- /dev/null +++ b/build.rs @@ -0,0 +1,21 @@ +include!("src/args.rs"); + +fn main() -> std::io::Result<()> { + // Tell Cargo that if the given file changes, to rerun this build script. + println!("cargo:rerun-if-changed=src/args.rs"); + + let out_dir = + std::path::PathBuf::from(std::env::var_os("OUT_DIR").ok_or(std::io::ErrorKind::NotFound)?); + + let cmd = ::command(); + + let man = clap_mangen::Man::new(cmd); + let mut buffer: Vec = Default::default(); + man.render(&mut buffer)?; + + println!("{:?}", buffer); + + std::fs::write(out_dir.join("ripsecrets.1"), buffer)?; + + Ok(()) +} diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..4ae9860 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,41 @@ +use clap::Parser; +use std::path::PathBuf; + +/// Prevent committing secret keys into your source code. +#[derive(Parser, Debug)] +#[clap( + version, + about, + name = "ripsecrets", + long_about = "ripsecrets searches files and directories recursively for secret API keys. +It's primarily designed to be used as a pre-commit to prevent committing +secrets into version control." +)] +struct Args { + /// Install `ripsecrets` as a pre-commit hook automatically in git directory provided. + #[clap(long = "install-pre-commit")] + install_pre_commit: bool, + + /// If you pass a path as an argument that's ignored by .secretsignore it + /// will be scanned by default. --strict-ignore will override this + /// behavior and not search the paths passed as arguments that are excluded + /// by the .secretsignore file. This is useful when invoking secrets as a + /// pre-commit. + #[clap(long = "strict-ignore")] + strict_ignore: bool, + + /// Print only the matched (non-empty) parts of a matching line, with each such + /// part on a separate output line. + #[clap(long = "only-matching")] + only_matching: bool, + + /// Additional regex patterns used to find secrets. If there is a matching + /// group in the regex the matched group will be tested for randomness before + /// being reported as a secret. + #[clap(long = "additional-pattern")] + additional_patterns: Vec, + + /// Source files. Can be files or directories. Defaults to '.' + #[clap(name = "Source files")] + paths: Vec, +} diff --git a/src/main.rs b/src/main.rs index eb6e4d5..2d3fffc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ #![allow(clippy::needless_return)] -use clap::Parser; use ripsecrets::find_secrets; -use std::path::PathBuf; use std::process; use termcolor::{BufferWriter, ColorChoice}; mod pre_commit; +include!("args.rs"); + #[derive(Debug)] pub enum UsageError { PreCommit, @@ -15,46 +15,6 @@ pub enum UsageError { Help, } -/// Prevent committing secret keys into your source code -#[derive(Parser, Debug)] -#[clap( - version, - about, - name = "ripsecrets", - long_about = "ripsecrets searches files and directories recursively for secret API keys. -It's primarily designed to be used as a pre-commit to prevent committing -secrets into version control." -)] -struct Args { - /// Install `ripsecrets` as a pre-commit hook automatically in git directory provided. Defaults to - /// '.' - #[clap(long = "install-pre-commit")] - install_pre_commit: bool, - - /// If you pass a path as an argument that's ignored by .secretsignore it - /// will be scanned by default. --strict-ignore will override this - /// behavior and not search the paths passed as arguments that are excluded - /// by the .secretsignore file. This is useful when invoking secrets as a - /// pre-commit. - #[clap(long = "strict-ignore")] - strict_ignore: bool, - - /// Print only the matched (non-empty) parts of a matching line, with each such - /// part on a separate output line. - #[clap(long = "only-matching")] - only_matching: bool, - - /// Additional regex patterns used to find secrets. If there is a matching - /// group in the regex the matched group will be tested for randomness before - /// being reported as a secret. - #[clap(long = "additional-pattern")] - additional_patterns: Vec, - - /// Source files. Can be files or directories. Defaults to '.' - #[clap(name = "Source files")] - paths: Vec, -} - enum RunResult { PreCommitInstallSuccessful, NoSecretsFound,