Skip to content

Commit 19ba912

Browse files
go/lint: move gofmt into golangci-lint and add ONLY_GOLANGCI flag (#325)
* Added the ONLY_GOLANGCI flag to only run the golangci-lint linter * move gofmt into golangci-lint * small change to how disabled and enabled linters get set, should not change any behavior * fixed config to set up formatter * Update go/lint-project.sh Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent fd9fe22 commit 19ba912

1 file changed

Lines changed: 26 additions & 57 deletions

File tree

go/lint-project.sh

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -40,55 +40,19 @@ else
4040
echo "running go linters for $OS_NAME"
4141
fi
4242

43-
# Check gofmt
44-
run_gofmt=true
45-
if [[ "$SKIP_LINTERS" != "" ]]; then
46-
run_gofmt=false
47-
fi
48-
if [[ "$OS_NAME" == "windows" ]]; then
49-
run_gofmt=false
50-
fi
51-
if [[ "$run_gofmt" == "true" ]]; then
52-
set +e
53-
code=0
54-
for file in "${GOFILES[@]}"
55-
do
56-
# Go 1.17 introduced a migration with build constraints
57-
# and they offer a migration with gofmt
58-
# See https://go.googlesource.com/proposal/+/master/design/draft-gobuild.md#transition for more details
59-
if [[ "$file" == "./pkged.go" ]];
60-
then
61-
gofmt -s -w pkged.go
62-
fi
63-
64-
# Check the file's formatting
65-
test -z $(gofmt -s -l $file)
66-
if [[ $? != 0 ]];
67-
then
68-
echo "DEBUG: formatting $file with gofmt"
69-
70-
test -z $(gofmt -s -w $file)
71-
if [[ $? != 0 ]];
72-
then
73-
echo "ERROR: problem rewriting $file"
74-
exit 1;
75-
fi
76-
fi
77-
done
78-
set -e
79-
if [[ $code != 0 ]];
80-
then
81-
exit $code
82-
fi
83-
84-
echo "finished gofmt check"
43+
# ONLY_GOLANGCI=yes skips all checks except golangci-lint (and its --fix via GOLANGCI_DO_FIX=true)
44+
if [[ "$ONLY_GOLANGCI" == "yes" ]]; then
45+
DISABLE_GITLEAKS=yes
46+
DISABLE_GOVULNCHECK=yes
47+
EXPERIMENTAL=""
48+
SKIP_TESTS=yes
8549
fi
8650

8751
# Would be set to 'moov-io' or 'moovfinancial'
8852
org=$(go mod why | head -n1 | awk -F'/' '{print $2}')
8953

9054
# Reject moovfinancial dependencies in moov-io projects
91-
if [[ "$org" == "moov-io" ]];
55+
if [[ "$ONLY_GOLANGCI" != "yes" && "$org" == "moov-io" ]];
9256
then
9357
# Fail our build if we find moovfinancial dependencies
9458
if go list -m all | grep moovfinancial;
@@ -115,6 +79,9 @@ then
11579
fi
11680

11781
# Verify no retracted module versions are in the build
82+
if [[ "$ONLY_GOLANGCI" == "yes" ]]; then
83+
retracted_mods=()
84+
else
11885
retracted_mods=($(go list -m -u all | grep retracted | cut -f1 -d' '))
11986
skip_modules=(
12087
"github.com/moby/sys/user"
@@ -145,9 +112,10 @@ do
145112
fi
146113
fi
147114
done
115+
fi
148116

149117
# Build the source code (to discover compile errors prior to linting)
150-
if [[ "$SKIP_LINTERS" == "" ]]; then
118+
if [[ "$SKIP_LINTERS" == "" && "$ONLY_GOLANGCI" != "yes" ]]; then
151119
echo "Building Go source code"
152120
go build $GORACE $GOTAGS $GOBUILD_FLAGS ./...
153121
echo "SUCCESS: Go code built without errors"
@@ -421,6 +389,12 @@ version: "2"
421389
run:
422390
tests: false
423391
go: "$GO_VERSION"
392+
formatters:
393+
enable:
394+
- gofmt
395+
settings:
396+
gofmt:
397+
simplify: true
424398
linters:
425399
default: none
426400
settings:
@@ -461,19 +435,14 @@ EOF
461435
echo " - pattern: ^fmt\.Print.*$" >> "$configFilepath"
462436
fi
463437

464-
cat <<EOF >> "$configFilepath"
465-
enable:
466-
- $(echo $enabled | sed 's/,/\n - /g')
467-
disable:
468-
- depguard
469-
- errcheck
470-
EOF
471-
if [[ "$DISABLED_GOLANGCI_LINTERS" != "" ]];
472-
then
473-
cat <<EOF >> "$configFilepath"
474-
- $(echo "$DISABLED_GOLANGCI_LINTERS" | sed 's/,/\n - /g')
475-
EOF
438+
# Build --enable and --disable flags from env vars rather than config
439+
GOLANGCI_ENABLE_FLAG="--enable=$enabled"
440+
441+
disabled="depguard,errcheck"
442+
if [[ "$DISABLED_GOLANGCI_LINTERS" != "" ]]; then
443+
disabled="$disabled,$DISABLED_GOLANGCI_LINTERS"
476444
fi
445+
GOLANGCI_DISABLE_FLAG="--disable=$disabled"
477446

478447
cat <<EOF >> "$configFilepath"
479448
exclusions:
@@ -509,7 +478,7 @@ EOF
509478
if [[ "$GOLANGCI_DO_FIX" == "true" ]]; then
510479
GOLANGCI_FIX_FLAG="--fix"
511480
fi
512-
./bin/golangci-lint $GOLANGCI_FLAGS run --config="$configFilepath" $GOLANGCI_FIX_FLAG --verbose --timeout=5m $GOLANGCI_TAGS
481+
./bin/golangci-lint $GOLANGCI_FLAGS run --config="$configFilepath" $GOLANGCI_FIX_FLAG $GOLANGCI_ENABLE_FLAG $GOLANGCI_DISABLE_FLAG --verbose --timeout=5m $GOLANGCI_TAGS
513482

514483
echo "FINISHED golangci-lint checks"
515484

0 commit comments

Comments
 (0)