Skip to content

Commit 04b3afc

Browse files
authored
Chore: Implement revive (grafana#16200)
Since we do not like some of the default golint rules, this commit proposes to use https://github.com/mgechev/revive. And potential revive speed-up should't hurt :). Right now, presented config (./conf/revive.toml) is permissive, we might improve it over time however. Fixes for found revive issues in the code are very limited so it wouldn't be large to review. Also in this commit: * Add annotations for makefile commands and declare phony targets * Rename "gometalinter" script and CI command to "lint" since we are doing there a bit more then using gometalinter package * Add Makefile rules to .editorconfig * Documentation which mentioned "golint" replaced with revive Fixes grafana#16109 Ref grafana#16160
1 parent 33d1d42 commit 04b3afc

File tree

10 files changed

+110
-27
lines changed

10 files changed

+110
-27
lines changed

.circleci/config.yml

+14-14
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
name: check documentation spelling errors
8787
command: 'codespell -I ./words_to_ignore.txt docs/'
8888

89-
gometalinter:
89+
backend-lint:
9090
docker:
9191
- image: circleci/golang:1.11.5
9292
environment:
@@ -96,8 +96,8 @@ jobs:
9696
steps:
9797
- checkout
9898
- run:
99-
name: Gometalinter tests
100-
command: './scripts/gometalinter.sh'
99+
name: backend lint
100+
command: './scripts/backend-lint.sh'
101101

102102
test-frontend:
103103
docker:
@@ -460,7 +460,7 @@ workflows:
460460
filters: *filter-only-master
461461
- codespell:
462462
filters: *filter-only-master
463-
- gometalinter:
463+
- backend-lint:
464464
filters: *filter-only-master
465465
- test-frontend:
466466
filters: *filter-only-master
@@ -476,7 +476,7 @@ workflows:
476476
- test-backend
477477
- test-frontend
478478
- codespell
479-
- gometalinter
479+
- backend-lint
480480
- mysql-integration-test
481481
- postgres-integration-test
482482
filters: *filter-only-master
@@ -487,7 +487,7 @@ workflows:
487487
- test-backend
488488
- test-frontend
489489
- codespell
490-
- gometalinter
490+
- backend-lint
491491
- mysql-integration-test
492492
- postgres-integration-test
493493
filters: *filter-only-master
@@ -497,7 +497,7 @@ workflows:
497497
- test-backend
498498
- test-frontend
499499
- codespell
500-
- gometalinter
500+
- backend-lint
501501
- mysql-integration-test
502502
- postgres-integration-test
503503
- build-all-enterprise
@@ -511,7 +511,7 @@ workflows:
511511
filters: *filter-only-release
512512
- codespell:
513513
filters: *filter-only-release
514-
- gometalinter:
514+
- backend-lint:
515515
filters: *filter-only-release
516516
- test-frontend:
517517
filters: *filter-only-release
@@ -527,7 +527,7 @@ workflows:
527527
- test-backend
528528
- test-frontend
529529
- codespell
530-
- gometalinter
530+
- backend-lint
531531
- mysql-integration-test
532532
- postgres-integration-test
533533
filters: *filter-only-release
@@ -538,7 +538,7 @@ workflows:
538538
- test-backend
539539
- test-frontend
540540
- codespell
541-
- gometalinter
541+
- backend-lint
542542
- mysql-integration-test
543543
- postgres-integration-test
544544
filters: *filter-only-release
@@ -549,7 +549,7 @@ workflows:
549549
- test-backend
550550
- test-frontend
551551
- codespell
552-
- gometalinter
552+
- backend-lint
553553
- mysql-integration-test
554554
- postgres-integration-test
555555
filters: *filter-only-release
@@ -560,7 +560,7 @@ workflows:
560560
filters: *filter-not-release-or-master
561561
- codespell:
562562
filters: *filter-not-release-or-master
563-
- gometalinter:
563+
- backend-lint:
564564
filters: *filter-not-release-or-master
565565
- test-frontend:
566566
filters: *filter-not-release-or-master
@@ -578,7 +578,7 @@ workflows:
578578
- test-backend
579579
- test-frontend
580580
- codespell
581-
- gometalinter
581+
- backend-lint
582582
- mysql-integration-test
583583
- postgres-integration-test
584584
- cache-server-test
@@ -589,7 +589,7 @@ workflows:
589589
- test-backend
590590
- test-frontend
591591
- codespell
592-
- gometalinter
592+
- backend-lint
593593
- mysql-integration-test
594594
- postgres-integration-test
595595
- cache-server-test

.editorconfig

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ insert_final_newline = true
1818

1919
[*.md]
2020
trim_trailing_whitespace = false
21+
22+
[Makefile]
23+
indent_style = tab
24+
indent_size = 2

Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
-include local/Makefile
22

3+
.PHONY: all deps-go deps-js deps build-go build-server build-cli build-js build build-docker-dev build-docker-full lint-go test-go test-js test run clean
4+
35
all: deps build
46

57
deps-go:
@@ -10,42 +12,57 @@ deps-js: node_modules
1012
deps: deps-js
1113

1214
build-go:
15+
@echo "build go files"
1316
go run build.go build
1417

1518
build-server:
19+
@echo "build server"
1620
go run build.go build-server
1721

1822
build-cli:
23+
@echo "build in CI environment"
1924
go run build.go build-cli
2025

2126
build-js:
27+
@echo "build frontend"
2228
yarn run build
2329

2430
build: build-go build-js
2531

2632
build-docker-dev:
33+
@echo "build development container"
2734
@echo "\033[92mInfo:\033[0m the frontend code is expected to be built already."
2835
go run build.go -goos linux -pkg-arch amd64 ${OPT} build pkg-archive latest
2936
cp dist/grafana-latest.linux-x64.tar.gz packaging/docker
3037
cd packaging/docker && docker build --tag grafana/grafana:dev .
3138

3239
build-docker-full:
40+
@echo "build docker container"
3341
docker build --tag grafana/grafana:dev .
3442

43+
lint-go:
44+
@echo "lint go source"
45+
scripts/backend-lint.sh
46+
3547
test-go:
48+
@echo "test backend"
3649
go test -v ./pkg/...
3750

3851
test-js:
52+
@echo "test frontend"
3953
yarn test
4054

4155
test: test-go test-js
4256

4357
run:
58+
@echo "start a server"
4459
./bin/grafana-server
4560

4661
clean:
62+
@echo "cleaning"
4763
rm -rf node_modules
4864
rm -rf public/build
4965

5066
node_modules: package.json yarn.lock
67+
@echo "install frontend dependencies"
5168
yarn install --pure-lockfile --no-progress

conf/revive.toml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
ignoreGeneratedHeader = false
2+
severity = "error"
3+
confidence = 0.8
4+
errorCode = 0
5+
6+
[rule.context-as-argument]
7+
[rule.error-return]
8+
[rule.package-comments]
9+
[rule.range]
10+
[rule.superfluous-else]
11+
[rule.modifies-parameter]
12+
13+
# This can be checked by other tools like megacheck
14+
[rule.unreachable-code]
15+
16+
# Those are probably should be enabled at some point
17+
# [rule.unexported-return]
18+
# [rule.exported]
19+
# [rule.var-naming]
20+
# [error-naming]
21+
# [rule.dot-imports]
22+
# [blank-imports]
23+
# [rule.receiver-naming]
24+
# [error-strings]
25+
# [rule.if-return]
26+
# [rule.indent-error-flow]
27+
# [rule.blank-imports]

grafana.deb

Whitespace-only changes.

packaging/conf/nfpm.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: "grafana"
2+
arch: "${ARCH}"
3+
platform: "linux"
4+
version: "${VERSION}"
5+
section: "default"
6+
priority: "extra"
7+
replaces:
8+
- grafana
9+
provides:
10+
- grafana-server
11+
- grafana-cli
12+
depends:
13+
- adduser
14+
- libfontconfig
15+
maintainer: "<[email protected]>"
16+
description: |
17+
Grafana
18+
vendor: "Grafana"
19+
homepage: "https://grafana.com"
20+
license: "Apache 2"
21+
bindir: "/usr/sbin"
22+
files:
23+
"./bin/grafana-server": "/usr/sbin/grafana-server"
24+
"./bin/grafana-cli": "/usr/sbin/grafana-cli"
25+
config_files:
26+
./packaging/deb/init.d/grafana-server: "/etc/init.d/grafana-server"
27+
./packaging/deb/default/grafana-server: "/etc/default/grafana-server"
28+
./packaging/deb/systemd/grafana-server.service: "/usr/lib/systemd/system/grafana-server.service"
29+
overrides:
30+
rpm:
31+
scripts:
32+
preinstall: ./scripts/preinstall.sh
33+
postremove: ./scripts/postremove.sh
34+
deb:
35+
scripts:
36+
postinstall: ./packaging/deb/control/postinst

pkg/cmd/grafana-cli/commands/commands.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func runDbCommand(command func(commandLine CommandLine) error) func(context *cli
3434

3535
cmd.ShowHelp()
3636
os.Exit(1)
37-
} else {
38-
logger.Info("\n\n")
3937
}
38+
39+
logger.Info("\n\n")
4040
}
4141
}
4242

@@ -50,9 +50,9 @@ func runPluginCommand(command func(commandLine CommandLine) error) func(context
5050

5151
cmd.ShowHelp()
5252
os.Exit(1)
53-
} else {
54-
logger.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n")
5553
}
54+
55+
logger.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n")
5656
}
5757
}
5858

pkg/infra/metrics/graphitebridge/graphite.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
// Package graphite provides a bridge to push Prometheus metrics to a Graphite
14+
// Package graphitebridge provides a bridge to push Prometheus metrics to a Graphite
1515
// server.
1616
package graphitebridge
1717

1818
import (
1919
"bufio"
20+
"context"
2021
"errors"
2122
"fmt"
2223
"io"
@@ -26,14 +27,10 @@ import (
2627
"strings"
2728
"time"
2829

29-
"context"
30-
30+
"github.com/prometheus/client_golang/prometheus"
31+
dto "github.com/prometheus/client_model/go"
3132
"github.com/prometheus/common/expfmt"
3233
"github.com/prometheus/common/model"
33-
34-
dto "github.com/prometheus/client_model/go"
35-
36-
"github.com/prometheus/client_golang/prometheus"
3734
)
3835

3936
const (

scripts/gometalinter.sh scripts/backend-lint.sh

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ go get -u github.com/opennota/check/cmd/structcheck
1919
go get -u github.com/mdempsky/unconvert
2020
go get -u github.com/opennota/check/cmd/varcheck
2121
go get -u honnef.co/go/tools/cmd/staticcheck
22+
go get -u github.com/mgechev/revive
2223

2324
exit_if_fail gometalinter --enable-gc --vendor --deadline 10m --disable-all \
2425
--enable=deadcode \
@@ -31,3 +32,4 @@ exit_if_fail gometalinter --enable-gc --vendor --deadline 10m --disable-all \
3132
--enable=staticcheck
3233

3334
exit_if_fail go vet ./pkg/...
35+
exit_if_fail revive -formatter stylish -config ./conf/revive.toml

style_guides/backend.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ In the `setting` packages there are many global variables which Grafana sets at
2020
away from and move as much configuration as possible to the `setting.Cfg` struct and pass it around, just like the bus.
2121

2222
## Linting and formatting
23-
We enforce strict `gofmt` formating and use some linters on our codebase. You can find the current list of linters at https://github.com/grafana/grafana/blob/master/scripts/gometalinter.sh#L23
23+
We enforce strict `gofmt` formating and use some linters on our codebase. You can find the current list of linters at https://github.com/grafana/grafana/blob/master/scripts/backend-lint.sh
2424

25-
We don't enforce `golint` but we encourage it and we will test so the number of linting errors does not increase over time.
25+
We use [revive](https://github.com/mgechev/revive) as a go linter, and do enforce our [custom config](https://github.com/grafana/grafana/blob/master/conf/revive.toml) for it.
2626

2727
## Testing
2828
We use GoConvey for BDD/scenario based testing. Which we think is useful for testing certain chain or interactions. Ex https://github.com/grafana/grafana/blob/master/pkg/services/auth/auth_token_test.go

0 commit comments

Comments
 (0)