-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
golangci-lint
executable file
·70 lines (62 loc) · 2.26 KB
/
golangci-lint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# Copyright 2019 The Vitess Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Required version of golangci-lint
REQUIRED_VERSION="v1.60.2"
# Function to compare versions in pure Bash
version_greater_or_equal() {
local IFS=.
local i
local ver1=($1)
local ver2=($2)
# Fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
ver1[i]=0
done
# Fill empty fields in ver2 with zeros
for ((i=${#ver2[@]}; i<${#ver1[@]}; i++)); do
ver2[i]=0
done
for ((i=0; i<${#ver1[@]}; i++)); do
if ((10#${ver1[i]} > 10#${ver2[i]})); then
return 0
elif ((10#${ver1[i]} < 10#${ver2[i]})); then
return 1
fi
done
return 0
}
# Check if golangci-lint is installed and capture the version
if ! command -v golangci-lint >/dev/null 2>&1; then
echo "golangci-lint not found. Installing version $REQUIRED_VERSION..."
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$REQUIRED_VERSION
else
VERSION_OUTPUT=$(golangci-lint --version)
INSTALLED_VERSION=$(echo "$VERSION_OUTPUT" | sed -n 's/^golangci-lint has version v\([0-9.]*\).*/\1/p')
if ! version_greater_or_equal "$INSTALLED_VERSION" "${REQUIRED_VERSION#v}"; then
echo "golangci-lint version $INSTALLED_VERSION found, but $REQUIRED_VERSION or newer is required."
echo "Installing version $REQUIRED_VERSION..."
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$REQUIRED_VERSION
fi
fi
# Get list of Go files to lint
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '^go/.*\.go$')
if [ -z "$gofiles" ]; then
exit 0
fi
# Get unique directories of the Go files
gopackages=$(echo "$gofiles" | xargs -n1 dirname | sort -u | paste -sd ' ' -)
# Lint the Go packages
echo "Linting $gopackages"
golangci-lint run $gopackages