Skip to content

Commit b36394b

Browse files
committed
Fix local imports, add appropriate linter
1. Modify Makefile's format target to format imports in three groups: - standard library packages; - third-party packages; - local packages (i.e. ones with the `github.com/google/cadvisor` prefix). Note goimports does the same job as gofmt, plus imports sorting. It can't do what gofmt -s does, but it was not done before (although this is checked according to .golangci.yml), so let's leave it as is. Note we don't have to use $(pkgs) and $(cmd_pkgs) in Makefile's format target, as goimports works recursively given the current directory, and it also skips vendor dir. 2. Add goimports linter with proper configuration to check imports formatting in CI. Check that the linter complains. 3. Actually implements these changes by running "make format". Check that the linter no longer complains. 4. Fix presubmit target to make sure it checks the new formatting rules. Instead of modifying build/check_gofmt.sh, let's use golangci-lint which already does this check (and more). While at it, let's remove vet target, as lint (golangci-lint) already calls it. Now, since make presumbit is already there in test job, the whole lint job is superseded by it, so let's remove it. While doing it, make sure we use the code from lint job to run make presubmit, with the following two exceptions: - "set -e" is not required, as it is set by GHA CI; - GOFLAGS="$GO_FLAGS" is not required, as it is done by Makefile. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 300242c commit b36394b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+129
-129
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
name: Test
22
on: [push, pull_request]
33
jobs:
4-
lint:
5-
strategy:
6-
matrix:
7-
environment-variables: [build/config/plain.sh, build/config/libpfm4.sh, build/config/libipmctl.sh]
8-
runs-on: ubuntu-20.04
9-
timeout-minutes: 10
10-
steps:
11-
- name: Install Go
12-
uses: actions/setup-go@v2
13-
with:
14-
go-version: 1.17
15-
- name: Checkout code
16-
uses: actions/checkout@v2
17-
- name: Run golangci-lint
18-
run: |
19-
set -e
20-
source ${{ matrix.environment-variables }}
21-
if [[ "${BUILD_PACKAGES}" != "" ]]; then sudo apt-get update; sudo apt-get install ${BUILD_PACKAGES}; fi
22-
make -e lint
234
test:
245
strategy:
256
matrix:
@@ -36,7 +17,10 @@ jobs:
3617
- name: Checkout code
3718
uses: actions/checkout@v2
3819
- name: Run presubmit checks
39-
run: GOFLAGS="$GO_FLAGS" make presubmit
20+
run: |
21+
source ${{ matrix.environment-variables }}
22+
if [[ "${BUILD_PACKAGES}" != "" ]]; then sudo apt-get update; sudo apt-get install ${BUILD_PACKAGES}; fi
23+
make -e presubmit
4024
- name: Run tests
4125
env:
4226
GOLANG_VERSION: ${{ matrix.go-versions }}

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ run:
77
min-confidence: 0
88
gofmt:
99
simplify: true
10+
goimports:
11+
local-prefixes: github.com/google/cadvisor
1012
linters:
1113
disable-all: true
1214
enable:
@@ -22,6 +24,7 @@ linters:
2224
- typecheck
2325
- golint
2426
- gofmt
27+
- goimports
2528
issues:
2629
max-issues-per-linter: 0
2730
max-same-issues: 0

Makefile

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,8 @@ tidy:
6262

6363
format:
6464
@echo ">> formatting code"
65-
@$(GO) fmt $(pkgs)
66-
@cd cmd && $(GO) fmt $(cmd_pkgs)
67-
68-
vet:
69-
@echo ">> vetting code"
70-
@$(GO) vet $(pkgs)
71-
@cd cmd && $(GO) vet $(cmd_pkgs)
65+
@# goimports is a superset of gofmt.
66+
@goimports -w -local github.com/google/cadvisor .
7267

7368
build: assets
7469
@echo ">> building binaries"
@@ -88,9 +83,7 @@ docker-%:
8883
docker-build:
8984
@docker run --rm -w /go/src/github.com/google/cadvisor -v ${PWD}:/go/src/github.com/google/cadvisor golang:1.17 make build
9085

91-
presubmit: vet
92-
@echo ">> checking go formatting"
93-
@./build/check_gofmt.sh
86+
presubmit: lint
9487
@echo ">> checking go mod tidy"
9588
@./build/check_gotidy.sh
9689
@echo ">> checking file boilerplate"
@@ -108,4 +101,4 @@ lint:
108101
clean:
109102
@rm -f *.test cadvisor
110103

111-
.PHONY: all build docker format release test test-integration vet lint presubmit tidy
104+
.PHONY: all build docker format release test test-integration lint presubmit tidy

accelerators/nvidia_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
package accelerators
1515

1616
import (
17-
"github.com/google/cadvisor/stats"
1817
"io/ioutil"
1918
"os"
2019
"path/filepath"
2120
"testing"
2221

22+
"github.com/google/cadvisor/stats"
23+
2324
"github.com/mindprince/gonvml"
2425
"github.com/stretchr/testify/assert"
2526
)

build/check_gofmt.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"path"
2929
"strings"
3030

31-
"github.com/google/cadvisor/info/v1"
31+
v1 "github.com/google/cadvisor/info/v1"
3232

3333
"k8s.io/klog/v2"
3434
)

client/v2/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"strings"
2828

2929
v1 "github.com/google/cadvisor/info/v1"
30-
"github.com/google/cadvisor/info/v2"
30+
v2 "github.com/google/cadvisor/info/v2"
3131
)
3232

3333
// Client represents the base URL for a cAdvisor client.

client/v2/client_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
"testing"
2525
"time"
2626

27-
"github.com/google/cadvisor/info/v1"
28-
"github.com/google/cadvisor/info/v2"
2927
"github.com/stretchr/testify/assert"
28+
29+
v1 "github.com/google/cadvisor/info/v1"
30+
v2 "github.com/google/cadvisor/info/v2"
3031
)
3132

3233
func cadvisorTestClient(path string, expectedPostObj *v1.ContainerInfoRequest, replyObj interface{}, t *testing.T) (*Client, *httptest.Server, error) {

cmd/cadvisor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
"flag"
1919
"testing"
2020

21-
"github.com/google/cadvisor/container"
2221
"github.com/stretchr/testify/assert"
22+
23+
"github.com/google/cadvisor/container"
2324
)
2425

2526
func TestTcpMetricsAreDisabledByDefault(t *testing.T) {

cmd/internal/container/mesos/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ package mesos
1616

1717
import (
1818
"fmt"
19+
"net/url"
20+
"sync"
21+
1922
"github.com/Rican7/retry"
2023
"github.com/Rican7/retry/strategy"
2124
mesos "github.com/mesos/mesos-go/api/v1/lib"
@@ -24,8 +27,6 @@ import (
2427
mclient "github.com/mesos/mesos-go/api/v1/lib/client"
2528
"github.com/mesos/mesos-go/api/v1/lib/encoding/codecs"
2629
"github.com/mesos/mesos-go/api/v1/lib/httpcli"
27-
"net/url"
28-
"sync"
2930
)
3031

3132
const (

0 commit comments

Comments
 (0)