hooked/hooked-cli/source/cli/run.rs

118 lines
3.2 KiB
Rust
Raw Normal View History

2022-10-31 14:20:38 +00:00
//! The `run` subcommand.
2022-10-30 20:14:12 +00:00
use std::{io::Read, process::exit};
use {
color_eyre::{eyre::eyre, Result},
hooked_config::{Config, ExitAction},
owo_colors::OwoColorize,
2022-10-30 20:14:12 +00:00
subprocess::{Exec, Redirection},
};
use crate::{
printer::{print, PrintType, PRINT_STYLE},
utilities::{globset_from_strings, plural},
};
2022-10-31 21:05:47 +00:00
2022-10-31 14:20:38 +00:00
/// The `run` subcommand.
2022-10-30 20:14:12 +00:00
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
let global_noise_level = &config.general.noise_level;
2022-10-30 20:14:12 +00:00
if hook_type == "pre-commit" {
2022-10-31 21:05:47 +00:00
let hook_count = config.pre_commit.len();
print(
format!(
"Hooked: Running {} pre-commit {}.",
hook_count,
plural(hook_count, "hook", None)
),
&config.general.noise_level,
PrintType::Info,
2022-10-31 10:36:55 +00:00
);
2022-11-20 11:39:08 +00:00
'hook_loop: for hook in config.pre_commit {
2022-10-30 20:14:12 +00:00
let hook_name = hook.name.unwrap_or_else(|| "Unnamed Hook".to_string());
2022-11-27 16:29:44 +00:00
if !hook.staged.is_empty() {
let globs = globset_from_strings(&hook.staged)?;
2022-11-20 11:39:08 +00:00
let staged_files = Exec::cmd("git")
.args(&["diff", "--name-only", "--cached"])
.capture()?
.stdout_str();
2022-11-20 11:57:58 +00:00
if !staged_files.lines().any(|line| globs.is_match(line)) {
print(
format!(
"\t{} {}",
"".style(PRINT_STYLE.skipped),
hook_name.style(PRINT_STYLE.skipped)
),
global_noise_level,
PrintType::Info,
2022-11-20 11:57:58 +00:00
);
continue 'hook_loop;
2022-11-20 11:39:08 +00:00
}
}
2022-10-30 20:14:12 +00:00
let command = match (hook.task.command, hook.task.script) {
(Some(command), _) => Ok(Exec::shell(command)),
(None, Some(script_file)) => {
let script_path = config.general.directory.join(script_file);
let script_path_str = script_path
.to_str()
.ok_or_else(|| eyre!("Failed to convert path to str"))?;
Ok(Exec::shell(script_path_str))
}
(None, None) => Err(eyre!(
"No command or script provided for hook: {}",
hook_name
)),
}?;
let mut process = command
.stderr(Redirection::Merge)
.stdout(Redirection::Pipe)
.popen()?;
2022-10-30 20:14:12 +00:00
let exit_status = process.wait()?;
let output = {
let mut output = String::new();
let mut stdout_file = process.stdout.take().unwrap();
stdout_file.read_to_string(&mut output)?;
output
};
let (stop, print_output, prefix, style, print_type) =
match (exit_status.success(), hook.on_failure) {
(true, _) => {
(false, false, "", PRINT_STYLE.success, PrintType::Info)
}
(false, ExitAction::Continue) => {
(false, true, "", PRINT_STYLE.warn, PrintType::Warn)
}
(false, ExitAction::Stop) => {
(true, true, "", PRINT_STYLE.error, PrintType::Error)
}
};
2022-10-30 20:14:12 +00:00
let hook_noise_level =
hook.noise_level.as_ref().unwrap_or(global_noise_level);
print(
format!("\t{} {}", prefix.style(style), hook_name.style(style)),
hook_noise_level,
print_type,
);
if !output.is_empty() && print_output {
print(output, hook_noise_level, PrintType::Info);
}
2022-10-30 20:14:12 +00:00
if stop {
exit(1);
}
}
}
Ok(())
}