Skip to content

Commit b588475

Browse files
authored
Merge pull request #380 from pohly/golangci-lint-action
golangci-lint action
2 parents edee20c + 1a0dfc5 commit b588475

33 files changed

+225
-184
lines changed

.github/workflows/lint.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Run lint
2+
3+
on: [ push, pull_request ]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
lint:
10+
strategy:
11+
matrix:
12+
path:
13+
- .
14+
- examples
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
- name: Lint
20+
uses: golangci/golangci-lint-action@v2
21+
with:
22+
# version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
23+
version: latest
24+
working-directory: ${{ matrix.path }}

.github/workflows/test.yml

-12
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ jobs:
2020
go test -v -race ./...
2121
- name: Test examples
2222
run: cd examples && go test -v -race ./...
23-
lint:
24-
runs-on: ubuntu-latest
25-
steps:
26-
- name: Install Go
27-
uses: actions/setup-go@v1
28-
- name: Checkout code
29-
uses: actions/checkout@v2
30-
- name: Lint
31-
run: |
32-
docker run --rm -v `pwd`:/go/src/k8s.io/klog -w /go/src/k8s.io/klog \
33-
golangci/golangci-lint:v1.50.1 golangci-lint run --disable-all -v \
34-
-E govet -E misspell -E gofmt -E ineffassign -E golint
3523
apidiff:
3624
runs-on: ubuntu-latest
3725
if: github.base_ref

.golangci.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters:
2+
disable-all: true
3+
enable: # sorted alphabetical
4+
- gofmt
5+
- misspell
6+
- revive

examples/benchmarks/benchmarks_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"go.uber.org/zap"
2828
"go.uber.org/zap/zapcore"
2929

30+
"k8s.io/klog/examples/util/require"
3031
"k8s.io/klog/v2"
3132
)
3233

@@ -38,11 +39,11 @@ func init() {
3839
// klog gets configured so that it writes to a single output file that
3940
// will be set during tests with SetOutput.
4041
klog.InitFlags(nil)
41-
flag.Set("v", fmt.Sprintf("%d", verbosityThreshold))
42-
flag.Set("log_file", "/dev/null")
43-
flag.Set("logtostderr", "false")
44-
flag.Set("alsologtostderr", "false")
45-
flag.Set("stderrthreshold", "10")
42+
require.NoError(flag.Set("v", fmt.Sprintf("%d", verbosityThreshold)))
43+
require.NoError(flag.Set("log_file", "/dev/null"))
44+
require.NoError(flag.Set("logtostderr", "false"))
45+
require.NoError(flag.Set("alsologtostderr", "false"))
46+
require.NoError(flag.Set("stderrthreshold", "10"))
4647
}
4748

4849
type testcase struct {

examples/coexist_glog/coexist_glog.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"flag"
55

66
"github.com/golang/glog"
7+
"k8s.io/klog/examples/util/require"
78
"k8s.io/klog/v2"
89
)
910

1011
func main() {
11-
flag.Set("alsologtostderr", "true")
12+
require.NoError(flag.Set("alsologtostderr", "true"))
1213
flag.Parse()
1314

1415
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
@@ -19,7 +20,7 @@ func main() {
1920
f2 := klogFlags.Lookup(f1.Name)
2021
if f2 != nil {
2122
value := f1.Value.String()
22-
f2.Value.Set(value)
23+
require.NoError(f2.Value.Set(value))
2324
}
2425
})
2526

examples/flushing/flushing_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66

77
"go.uber.org/goleak"
8+
9+
"k8s.io/klog/examples/util/require"
810
"k8s.io/klog/v2"
911
)
1012

@@ -13,8 +15,8 @@ func main() {
1315

1416
// By default klog writes to stderr. Setting logtostderr to false makes klog
1517
// write to a log file.
16-
flag.Set("logtostderr", "false")
17-
flag.Set("log_file", "myfile.log")
18+
require.NoError(flag.Set("logtostderr", "false"))
19+
require.NoError(flag.Set("log_file", "myfile.log"))
1820
flag.Parse()
1921

2022
// Info writes the first log message. When the first log file is created,

examples/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module example
1+
module k8s.io/klog/examples
22

33
go 1.13
44

examples/klogr/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"flag"
55

6+
"k8s.io/klog/examples/util/require"
67
"k8s.io/klog/v2"
78
"k8s.io/klog/v2/klogr"
89
)
@@ -17,7 +18,7 @@ func (e myError) Error() string {
1718

1819
func main() {
1920
klog.InitFlags(nil)
20-
flag.Set("v", "3")
21+
require.NoError(flag.Set("v", "3"))
2122
flag.Parse()
2223
log := klogr.New().WithName("MyName").WithValues("user", "you")
2324
log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})

examples/log_file/usage_log_file.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"flag"
55

6+
"k8s.io/klog/examples/util/require"
67
"k8s.io/klog/v2"
78
)
89

910
func main() {
1011
klog.InitFlags(nil)
1112
// By default klog writes to stderr. Setting logtostderr to false makes klog
1213
// write to a log file.
13-
flag.Set("logtostderr", "false")
14-
flag.Set("log_file", "myfile.log")
14+
require.NoError(flag.Set("logtostderr", "false"))
15+
require.NoError(flag.Set("log_file", "myfile.log"))
1516
flag.Parse()
1617
klog.Info("nice to meet you")
1718
klog.Flush()

examples/output_test/output_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ import (
3434
"k8s.io/klog/v2/textlogger"
3535
)
3636

37-
func newLogger(out io.Writer, v int, vmodule string) logr.Logger {
37+
// newLogger is a test.OutputConfig.NewLogger callback which creates a zapr
38+
// logger. The vmodule parameter is ignored because zapr does not support that.
39+
func newLogger(out io.Writer, v int, _ string) logr.Logger {
3840
return newZaprLogger(out, v)
3941
}
4042

examples/set_output/usage_set_output.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bytes"
55
"flag"
66
"fmt"
7+
8+
"k8s.io/klog/examples/util/require"
79
"k8s.io/klog/v2"
810
)
911

1012
func main() {
1113
klog.InitFlags(nil)
12-
flag.Set("logtostderr", "false")
13-
flag.Set("alsologtostderr", "false")
14+
require.NoError(flag.Set("logtostderr", "false"))
15+
require.NoError(flag.Set("alsologtostderr", "false"))
1416
flag.Parse()
1517

1618
buf := new(bytes.Buffer)

examples/structured_logging/structured_logging.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func main() {
2525
flag.Parse()
2626

2727
someData := MyStruct{
28-
Name: "hello",
29-
Data: "world",
28+
Name: "hello",
29+
Data: "world",
30+
internal: 42,
3031
}
3132

3233
longData := MyStruct{

examples/util/require/require.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package require
2+
3+
func NoError(err error) {
4+
if err != nil {
5+
panic(err)
6+
}
7+
}

exit_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ func ExampleFlushAndExit() {
2727

2828
var fs flag.FlagSet
2929
klog.InitFlags(&fs)
30-
fs.Set("skip_headers", "true")
31-
defer flag.Set("skip_headers", "false")
32-
fs.Set("logtostderr", "false")
33-
defer fs.Set("logtostderr", "true")
30+
state := klog.CaptureState()
31+
defer state.Restore()
32+
if err := fs.Set("skip_headers", "true"); err != nil {
33+
panic(err)
34+
}
35+
if err := fs.Set("logtostderr", "false"); err != nil {
36+
panic(err)
37+
}
3438
klog.SetOutput(os.Stdout)
35-
defer klog.SetOutput(nil)
3639
klog.OsExit = func(exitCode int) {
3740
fmt.Printf("os.Exit(%d)\n", exitCode)
3841
}

format_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestFormat(t *testing.T) {
4343
`, klog.Format(obj).(fmt.Stringer).String(), "Format(config).String()")
4444
// fmt.Sprintf would call String if it was available.
4545
str := fmt.Sprintf("%s", klog.Format(obj).(logr.Marshaler).MarshalLog())
46-
if strings.Index(str, "kind is config") >= 0 {
46+
if strings.Contains(str, "kind is config") {
4747
t.Errorf("fmt.Sprintf called TypeMeta.String for klog.Format(obj).MarshalLog():\n%s", str)
4848
}
4949

internal/buffer/buffer.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ var (
3636
// use. It also provides some helper methods for output formatting.
3737
type Buffer struct {
3838
bytes.Buffer
39-
Tmp [64]byte // temporary byte array for creating headers.
40-
next *Buffer
39+
Tmp [64]byte // temporary byte array for creating headers.
4140
}
4241

4342
var buffers = sync.Pool{

internal/clock/clock.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,6 @@ type Clock interface {
3939
// Sleep sleeps for the provided duration d.
4040
// Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
4141
Sleep(d time.Duration)
42-
// Tick returns the channel of a new Ticker.
43-
// This method does not allow to free/GC the backing ticker. Use
44-
// NewTicker from WithTicker instead.
45-
Tick(d time.Duration) <-chan time.Time
46-
}
47-
48-
// WithTicker allows for injecting fake or real clocks into code that
49-
// needs to do arbitrary things based on time.
50-
type WithTicker interface {
51-
Clock
5242
// NewTicker returns a new Ticker.
5343
NewTicker(time.Duration) Ticker
5444
}
@@ -66,7 +56,7 @@ type WithDelayedExecution interface {
6656
// WithTickerAndDelayedExecution allows for injecting fake or real clocks
6757
// into code that needs Ticker and AfterFunc functionality
6858
type WithTickerAndDelayedExecution interface {
69-
WithTicker
59+
Clock
7060
// AfterFunc executes f in its own goroutine after waiting
7161
// for d duration and returns a Timer whose channel can be
7262
// closed by calling Stop() on the Timer.
@@ -79,7 +69,7 @@ type Ticker interface {
7969
Stop()
8070
}
8171

82-
var _ = WithTicker(RealClock{})
72+
var _ Clock = RealClock{}
8373

8474
// RealClock really calls time.Now()
8575
type RealClock struct{}
@@ -115,13 +105,6 @@ func (RealClock) AfterFunc(d time.Duration, f func()) Timer {
115105
}
116106
}
117107

118-
// Tick is the same as time.Tick(d)
119-
// This method does not allow to free/GC the backing ticker. Use
120-
// NewTicker instead.
121-
func (RealClock) Tick(d time.Duration) <-chan time.Time {
122-
return time.Tick(d)
123-
}
124-
125108
// NewTicker returns a new Ticker.
126109
func (RealClock) NewTicker(d time.Duration) Ticker {
127110
return &realTicker{

internal/clock/testing/fake_clock.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
var (
2727
_ = clock.PassiveClock(&FakePassiveClock{})
28-
_ = clock.WithTicker(&FakeClock{})
2928
_ = clock.Clock(&IntervalClock{})
3029
)
3130

@@ -259,36 +258,30 @@ func (i *IntervalClock) Since(ts time.Time) time.Duration {
259258

260259
// After is unimplemented, will panic.
261260
// TODO: make interval clock use FakeClock so this can be implemented.
262-
func (*IntervalClock) After(d time.Duration) <-chan time.Time {
261+
func (*IntervalClock) After(time.Duration) <-chan time.Time {
263262
panic("IntervalClock doesn't implement After")
264263
}
265264

266265
// NewTimer is unimplemented, will panic.
267266
// TODO: make interval clock use FakeClock so this can be implemented.
268-
func (*IntervalClock) NewTimer(d time.Duration) clock.Timer {
267+
func (*IntervalClock) NewTimer(time.Duration) clock.Timer {
269268
panic("IntervalClock doesn't implement NewTimer")
270269
}
271270

272271
// AfterFunc is unimplemented, will panic.
273272
// TODO: make interval clock use FakeClock so this can be implemented.
274-
func (*IntervalClock) AfterFunc(d time.Duration, f func()) clock.Timer {
273+
func (*IntervalClock) AfterFunc(time.Duration, func()) clock.Timer {
275274
panic("IntervalClock doesn't implement AfterFunc")
276275
}
277276

278-
// Tick is unimplemented, will panic.
279-
// TODO: make interval clock use FakeClock so this can be implemented.
280-
func (*IntervalClock) Tick(d time.Duration) <-chan time.Time {
281-
panic("IntervalClock doesn't implement Tick")
282-
}
283-
284277
// NewTicker has no implementation yet and is omitted.
285278
// TODO: make interval clock use FakeClock so this can be implemented.
286-
func (*IntervalClock) NewTicker(d time.Duration) clock.Ticker {
279+
func (*IntervalClock) NewTicker(time.Duration) clock.Ticker {
287280
panic("IntervalClock doesn't implement NewTicker")
288281
}
289282

290283
// Sleep is unimplemented, will panic.
291-
func (*IntervalClock) Sleep(d time.Duration) {
284+
func (*IntervalClock) Sleep(time.Duration) {
292285
panic("IntervalClock doesn't implement Sleep")
293286
}
294287

internal/test/require/require.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package require
18+
19+
import "testing"
20+
21+
func NoError(tb testing.TB, err error) {
22+
if err != nil {
23+
tb.Helper()
24+
tb.Fatalf("Unexpected error: %v", err)
25+
}
26+
}

internal/verbosity/verbosity.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,7 @@ func (vs *VState) setV(pc uintptr) Level {
288288
fn := runtime.FuncForPC(pc)
289289
file, _ := fn.FileLine(pc)
290290
// The file is something like /a/b/c/d.go. We want just the d.
291-
if strings.HasSuffix(file, ".go") {
292-
file = file[:len(file)-3]
293-
}
291+
file = strings.TrimSuffix(file, ".go")
294292
if slash := strings.LastIndex(file, "/"); slash >= 0 {
295293
file = file[slash+1:]
296294
}

0 commit comments

Comments
 (0)