Skip to content

Commit e6dbd4e

Browse files
authored
Merge pull request #20 from valentin-krasontovitsch/let-staticcheck-take-args
Let staticcheck take args
2 parents 214fe94 + d2d8975 commit e6dbd4e

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

.pre-commit-hooks.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
files: '\.go$'
4747
language: "script"
4848
description: "Runs `staticcheck`, install https://staticcheck.io/docs/"
49+
require_serial: true
4950

5051
- id: golangci-lint
5152
name: "go golangci-lint"

run-go-static-check.sh

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

3-
for DIR in $(echo "$@"|xargs -n1 dirname|sort -u); do
4-
staticcheck ./"$DIR"
3+
set -euo pipefail
4+
5+
cmd_options=()
6+
file_args=()
7+
8+
# we parse out cli options and file arguments
9+
# technically we allow for options to be passed after the file arguments, but
10+
# we know that pre-commit puts the file arguments last
11+
# also, we do not handle file names starting with a hyphen
12+
while [[ $# -gt 0 ]]; do
13+
if [[ "$1" == -* ]]; then
14+
# If it's an option, ensure there's a value after it
15+
if [[ $# -ge 2 && -n "$2" && "$2" != -* ]]; then
16+
cmd_options+=("$1" "$2")
17+
shift 2
18+
else
19+
echo "Error: Missing value for key $1"
20+
exit 1
21+
fi
22+
else
23+
file_args+=("$1")
24+
shift
25+
fi
26+
done
27+
28+
if [[ ${#file_args[@]} -eq 0 ]]; then
29+
echo "Error: No file arguments provided"
30+
exit 1
31+
fi
32+
33+
packages=()
34+
35+
for DIR in $(echo "${file_args[@]}" | xargs -n1 dirname | sort -u); do
36+
packages+=("$DIR")
37+
done
38+
39+
[[ ${#cmd_options[@]} -eq 0 ]] && cmd_options+=("")
40+
41+
for package in "${packages[@]}"; do
42+
staticcheck "${cmd_options[@]}" ./"${package}"
543
done

0 commit comments

Comments
 (0)