#![allow(clippy::needless_return)]
use ripsecrets::find_secrets;
use std::process;
use termcolor::{BufferWriter, ColorChoice};
mod pre_commit;
include!("args.rs");
#[derive(Debug)]
pub enum UsageError {
PreCommit,
Version,
Help,
}
enum RunResult {
PreCommitInstallSuccessful,
NoSecretsFound,
SecretsFound,
Error(String),
}
fn main() {
match run() {
RunResult::PreCommitInstallSuccessful => process::exit(0),
RunResult::NoSecretsFound => process::exit(0),
RunResult::SecretsFound => process::exit(1),
RunResult::Error(e) => {
eprintln!("Error: {}", e);
process::exit(2)
}
}
}
fn run() -> RunResult {
let args = Args::parse();
let paths = if args.paths.is_empty() {
vec![PathBuf::from(".")]
} else {
args.paths
};
if args.install_pre_commit && (args.strict_ignore || args.only_matching) {
let option = if args.strict_ignore {
"--strict-ignore"
} else {
"--only-matching"
};
return RunResult::Error(format!(
"{} is not a valid option when installing pre-commits. Use --install-pre-commit alone",
option
));
}
if args.install_pre_commit {
for path in paths {
match pre_commit::install_pre_commit(&path) {
Ok(()) => (),
Err(err) => {
return RunResult::Error(err.to_string());
}
}
}
RunResult::PreCommitInstallSuccessful
} else {
match find_secrets(
&paths,
&args.additional_patterns,
args.strict_ignore,
args.only_matching,
BufferWriter::stdout(ColorChoice::Never),
) {
Ok(0) => RunResult::NoSecretsFound,
Ok(_num_secrets) => RunResult::SecretsFound,
Err(err) => RunResult::Error(err.to_string()),
}
}
}