Skip to content

Commit

Permalink
Generate manpage as part of build (#83)
Browse files Browse the repository at this point in the history
* Generate manpage via `clap_mangen`

* Cleanup CLI docstrings
  • Loading branch information
lafrenierejm committed Jan 16, 2024
1 parent 6b3d1aa commit 07e705b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 42 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
21 changes: 21 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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 = <Args as clap::CommandFactory>::command();

let man = clap_mangen::Man::new(cmd);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;

println!("{:?}", buffer);

std::fs::write(out_dir.join("ripsecrets.1"), buffer)?;

Ok(())
}
41 changes: 41 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -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<String>,

/// Source files. Can be files or directories. Defaults to '.'
#[clap(name = "Source files")]
paths: Vec<PathBuf>,
}
44 changes: 2 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,20 @@
#![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,
Version,
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<String>,

/// Source files. Can be files or directories. Defaults to '.'
#[clap(name = "Source files")]
paths: Vec<PathBuf>,
}

enum RunResult {
PreCommitInstallSuccessful,
NoSecretsFound,
Expand Down

0 comments on commit 07e705b

Please sign in to comment.