1
0
mirror of https://github.com/j178/prek.git synced 2026-04-25 02:11:36 +02:00

Use anyhow::Context (#303)

This commit is contained in:
Jo
2025-08-02 12:36:58 +08:00
committed by GitHub
parent e2465ec7e5
commit eb8ba0941d
7 changed files with 14 additions and 11 deletions
@@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet};
use anyhow::Context;
use clap::Parser;
use futures::StreamExt;
@@ -34,7 +35,7 @@ pub(crate) async fn check_added_large_files(
filenames: &[&String],
_env_vars: &HashMap<&'static str, String>,
) -> anyhow::Result<(i32, Vec<u8>)> {
let entry = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?;
let entry = shlex::split(&hook.entry).context("Failed to parse entry")?;
let args = Args::try_parse_from(entry.iter().chain(&hook.args))?;
let filter = if args.enforce_all {
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::path::Path;
use anyhow::Result;
use anyhow::{Context, Result};
use bstr::ByteSlice;
use clap::Parser;
use futures::StreamExt;
@@ -22,7 +22,7 @@ pub(crate) async fn fix_trailing_whitespace(
filenames: &[&String],
_env_vars: &HashMap<&'static str, String>,
) -> Result<(i32, Vec<u8>)> {
let entry = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?;
let entry = shlex::split(&hook.entry).context("Failed to parse entry")?;
let args = Args::try_parse_from(entry.iter().chain(&hook.args))?;
let force_markdown = args.markdown_linebreak_ext.iter().any(|ext| ext == "*");
+1 -1
View File
@@ -204,7 +204,7 @@ impl LanguageImpl for Docker {
let docker_tag = Docker::docker_tag(hook);
let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?;
let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?;
let run = async move |batch: Vec<String>| {
// docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
+2 -3
View File
@@ -96,10 +96,9 @@ impl LanguageImpl for Node {
env_vars: &HashMap<&'static str, String>,
_store: &Store,
) -> Result<(i32, Vec<u8>), Error> {
let env_dir = hook.env_path().expect("Python must have env path");
let env_dir = hook.env_path().expect("Node must have env path");
// TODO: move split to hook construction
let cmds = shlex::split(&hook.entry)
.ok_or_else(|| anyhow::anyhow!("Failed to parse entry command"))?;
let cmds = shlex::split(&hook.entry).context("Failed to parse entry command")?;
let new_path = prepend_path(&bin_dir(env_dir)).context("Failed to join PATH")?;
+1 -2
View File
@@ -130,8 +130,7 @@ impl LanguageImpl for Python {
_store: &Store,
) -> Result<(i32, Vec<u8>), Error> {
let env_dir = hook.env_path().expect("Python must have env path");
let cmds = shlex::split(&hook.entry)
.ok_or_else(|| anyhow::anyhow!("Failed to parse entry command"))?;
let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?;
// Construct PATH with venv bin directory first
let new_path = prepend_path(&bin_dir(env_dir)).context("Failed to join PATH")?;
+3 -1
View File
@@ -1,5 +1,7 @@
use std::collections::HashMap;
use anyhow::Context;
use crate::hook::Hook;
use crate::hook::InstalledHook;
use crate::languages::{Error, LanguageImpl};
@@ -26,7 +28,7 @@ impl LanguageImpl for Script {
env_vars: &HashMap<&'static str, String>,
_store: &Store,
) -> Result<(i32, Vec<u8>), Error> {
let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?;
let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?;
let run = async move |batch: Vec<String>| {
let mut command = Cmd::new(&cmds[0], "run script command")
+3 -1
View File
@@ -1,5 +1,7 @@
use std::collections::HashMap;
use anyhow::Context;
use crate::hook::{Hook, InstalledHook};
use crate::languages::{Error, LanguageImpl};
use crate::process::Cmd;
@@ -25,7 +27,7 @@ impl LanguageImpl for System {
env_vars: &HashMap<&'static str, String>,
_store: &Store,
) -> Result<(i32, Vec<u8>), Error> {
let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?;
let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?;
let run = async move |batch: Vec<String>| {
let mut output = Cmd::new(&cmds[0], "run system command")