★ pre-commit: create hooks for unsupported tools
• 186 words • 1 min • updated
When using pre-commit.com, in an ideal world, every
formatter / linter / code analyzer would have a .pre-commit-config.yaml file
in its repository root.
In the real world, that’s not always the case.
A recent example: cloudflare/pint:
Prometheus rule linter/validator
It is a golang binary that lints prometheus rules.
Can we bridge the gap?
The end goal is the ability to run pre-commit run --all-files pint in our git
repository.
For that, we’ll need to define a local / custom hook in our
~/.pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: pint
name: Validate prometheus rules with pint
language: golang
additional_dependencies:
- github.com/cloudflare/pint/cmd/pint@v0.69.1
entry: pint --offline lint
types:
- yaml
files: ^helm/clustermon-alerts/rules/That simply works™. How did I get there? Starting with the references:
- https://adamj.eu/tech/2023/02/09/pre-commit-hooks-unsupported-tools/
- https://perrotta.dev/2024/11/pre-commit-additional-dependencies-in-golang/
- https://stackoverflow.com/questions/62974985/go-module-latest-found-but-does-not-contain-package
- https://pkg.go.dev/github.com/cloudflare/pint@v0.69.1/cmd/pint
The key part is language: golang + additional_dependencies.
Specifying additional_dependencies is a bit tricky. Initially, I did:
github.com/cloudflare/pint@v0.69.1…but that yields nothing. In fact:
% go run github.com/cloudflare/pint@v0.69.1
go: github.com/cloudflare/pint@v0.69.1: module github.com/cloudflare/pint@v0.69.1 found, but does not contain package github.com/cloudflare/pintWe must always specify the module that contains the main function with go run, and that is cmd/pint.
Backlinks
- Pre-commit (Dec 21, 2024)