Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golangci-lint action #380

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Run lint

on: [ push, pull_request ]

permissions:
contents: read

jobs:
lint:
strategy:
matrix:
path:
- .
- examples
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
# version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
working-directory: ${{ matrix.path }}
12 changes: 0 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ jobs:
go test -v -race ./...
- name: Test examples
run: cd examples && go test -v -race ./...
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v1
- name: Checkout code
uses: actions/checkout@v2
- name: Lint
run: |
docker run --rm -v `pwd`:/go/src/k8s.io/klog -w /go/src/k8s.io/klog \
golangci/golangci-lint:v1.50.1 golangci-lint run --disable-all -v \
-E govet -E misspell -E gofmt -E ineffassign -E golint
apidiff:
runs-on: ubuntu-latest
if: github.base_ref
Expand Down
6 changes: 6 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters:
disable-all: true
enable: # sorted alphabetical
- gofmt
- misspell
- revive
11 changes: 6 additions & 5 deletions examples/benchmarks/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"k8s.io/klog/examples/util/require"
"k8s.io/klog/v2"
)

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

type testcase struct {
Expand Down
5 changes: 3 additions & 2 deletions examples/coexist_glog/coexist_glog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"flag"

"github.com/golang/glog"
"k8s.io/klog/examples/util/require"
"k8s.io/klog/v2"
)

func main() {
flag.Set("alsologtostderr", "true")
require.NoError(flag.Set("alsologtostderr", "true"))
flag.Parse()

klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
Expand All @@ -19,7 +20,7 @@ func main() {
f2 := klogFlags.Lookup(f1.Name)
if f2 != nil {
value := f1.Value.String()
f2.Value.Set(value)
require.NoError(f2.Value.Set(value))
}
})

Expand Down
6 changes: 4 additions & 2 deletions examples/flushing/flushing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"go.uber.org/goleak"

"k8s.io/klog/examples/util/require"
"k8s.io/klog/v2"
)

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

// By default klog writes to stderr. Setting logtostderr to false makes klog
// write to a log file.
flag.Set("logtostderr", "false")
flag.Set("log_file", "myfile.log")
require.NoError(flag.Set("logtostderr", "false"))
require.NoError(flag.Set("log_file", "myfile.log"))
flag.Parse()

// Info writes the first log message. When the first log file is created,
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module example
module k8s.io/klog/examples

go 1.13

Expand Down
3 changes: 2 additions & 1 deletion examples/klogr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"

"k8s.io/klog/examples/util/require"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
)
Expand All @@ -17,7 +18,7 @@ func (e myError) Error() string {

func main() {
klog.InitFlags(nil)
flag.Set("v", "3")
require.NoError(flag.Set("v", "3"))
flag.Parse()
log := klogr.New().WithName("MyName").WithValues("user", "you")
log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
Expand Down
5 changes: 3 additions & 2 deletions examples/log_file/usage_log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package main
import (
"flag"

"k8s.io/klog/examples/util/require"
"k8s.io/klog/v2"
)

func main() {
klog.InitFlags(nil)
// By default klog writes to stderr. Setting logtostderr to false makes klog
// write to a log file.
flag.Set("logtostderr", "false")
flag.Set("log_file", "myfile.log")
require.NoError(flag.Set("logtostderr", "false"))
require.NoError(flag.Set("log_file", "myfile.log"))
flag.Parse()
klog.Info("nice to meet you")
klog.Flush()
Expand Down
4 changes: 3 additions & 1 deletion examples/output_test/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import (
"k8s.io/klog/v2/textlogger"
)

func newLogger(out io.Writer, v int, vmodule string) logr.Logger {
// newLogger is a test.OutputConfig.NewLogger callback which creates a zapr
// logger. The vmodule parameter is ignored because zapr does not support that.
func newLogger(out io.Writer, v int, _ string) logr.Logger {
pohly marked this conversation as resolved.
Show resolved Hide resolved
return newZaprLogger(out, v)
}

Expand Down
6 changes: 4 additions & 2 deletions examples/set_output/usage_set_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"flag"
"fmt"

"k8s.io/klog/examples/util/require"
"k8s.io/klog/v2"
)

func main() {
klog.InitFlags(nil)
flag.Set("logtostderr", "false")
flag.Set("alsologtostderr", "false")
require.NoError(flag.Set("logtostderr", "false"))
require.NoError(flag.Set("alsologtostderr", "false"))
flag.Parse()

buf := new(bytes.Buffer)
Expand Down
5 changes: 3 additions & 2 deletions examples/structured_logging/structured_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func main() {
flag.Parse()

someData := MyStruct{
Name: "hello",
Data: "world",
Name: "hello",
Data: "world",
internal: 42,
}

longData := MyStruct{
Expand Down
7 changes: 7 additions & 0 deletions examples/util/require/require.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package require

func NoError(err error) {
if err != nil {
panic(err)
}
}
13 changes: 8 additions & 5 deletions exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ func ExampleFlushAndExit() {

var fs flag.FlagSet
klog.InitFlags(&fs)
fs.Set("skip_headers", "true")
defer flag.Set("skip_headers", "false")
fs.Set("logtostderr", "false")
defer fs.Set("logtostderr", "true")
state := klog.CaptureState()
defer state.Restore()
if err := fs.Set("skip_headers", "true"); err != nil {
panic(err)
}
if err := fs.Set("logtostderr", "false"); err != nil {
panic(err)
pohly marked this conversation as resolved.
Show resolved Hide resolved
}
klog.SetOutput(os.Stdout)
defer klog.SetOutput(nil)
klog.OsExit = func(exitCode int) {
fmt.Printf("os.Exit(%d)\n", exitCode)
}
Expand Down
2 changes: 1 addition & 1 deletion format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestFormat(t *testing.T) {
`, klog.Format(obj).(fmt.Stringer).String(), "Format(config).String()")
// fmt.Sprintf would call String if it was available.
str := fmt.Sprintf("%s", klog.Format(obj).(logr.Marshaler).MarshalLog())
if strings.Index(str, "kind is config") >= 0 {
if strings.Contains(str, "kind is config") {
t.Errorf("fmt.Sprintf called TypeMeta.String for klog.Format(obj).MarshalLog():\n%s", str)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ var (
// use. It also provides some helper methods for output formatting.
type Buffer struct {
bytes.Buffer
Tmp [64]byte // temporary byte array for creating headers.
next *Buffer
Tmp [64]byte // temporary byte array for creating headers.
}

var buffers = sync.Pool{
Expand Down
21 changes: 2 additions & 19 deletions internal/clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ type Clock interface {
// Sleep sleeps for the provided duration d.
// Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
Sleep(d time.Duration)
// Tick returns the channel of a new Ticker.
// This method does not allow to free/GC the backing ticker. Use
// NewTicker from WithTicker instead.
Tick(d time.Duration) <-chan time.Time
}

// WithTicker allows for injecting fake or real clocks into code that
// needs to do arbitrary things based on time.
type WithTicker interface {
Clock
// NewTicker returns a new Ticker.
NewTicker(time.Duration) Ticker
}
Expand All @@ -66,7 +56,7 @@ type WithDelayedExecution interface {
// WithTickerAndDelayedExecution allows for injecting fake or real clocks
// into code that needs Ticker and AfterFunc functionality
type WithTickerAndDelayedExecution interface {
WithTicker
Clock
// AfterFunc executes f in its own goroutine after waiting
// for d duration and returns a Timer whose channel can be
// closed by calling Stop() on the Timer.
Expand All @@ -79,7 +69,7 @@ type Ticker interface {
Stop()
}

var _ = WithTicker(RealClock{})
var _ Clock = RealClock{}

// RealClock really calls time.Now()
type RealClock struct{}
Expand Down Expand Up @@ -115,13 +105,6 @@ func (RealClock) AfterFunc(d time.Duration, f func()) Timer {
}
}

// Tick is the same as time.Tick(d)
// This method does not allow to free/GC the backing ticker. Use
// NewTicker instead.
func (RealClock) Tick(d time.Duration) <-chan time.Time {
return time.Tick(d)
}

// NewTicker returns a new Ticker.
func (RealClock) NewTicker(d time.Duration) Ticker {
return &realTicker{
Expand Down
17 changes: 5 additions & 12 deletions internal/clock/testing/fake_clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

var (
_ = clock.PassiveClock(&FakePassiveClock{})
_ = clock.WithTicker(&FakeClock{})
_ = clock.Clock(&IntervalClock{})
)

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

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

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

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

// Tick is unimplemented, will panic.
// TODO: make interval clock use FakeClock so this can be implemented.
func (*IntervalClock) Tick(d time.Duration) <-chan time.Time {
panic("IntervalClock doesn't implement Tick")
}

// NewTicker has no implementation yet and is omitted.
// TODO: make interval clock use FakeClock so this can be implemented.
func (*IntervalClock) NewTicker(d time.Duration) clock.Ticker {
func (*IntervalClock) NewTicker(time.Duration) clock.Ticker {
panic("IntervalClock doesn't implement NewTicker")
}

// Sleep is unimplemented, will panic.
func (*IntervalClock) Sleep(d time.Duration) {
func (*IntervalClock) Sleep(time.Duration) {
panic("IntervalClock doesn't implement Sleep")
}

Expand Down
26 changes: 26 additions & 0 deletions internal/test/require/require.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2023 The Kubernetes 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.
*/

package require

import "testing"

func NoError(tb testing.TB, err error) {
if err != nil {
tb.Helper()
tb.Fatalf("Unexpected error: %v", err)
}
}
4 changes: 1 addition & 3 deletions internal/verbosity/verbosity.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ func (vs *VState) setV(pc uintptr) Level {
fn := runtime.FuncForPC(pc)
file, _ := fn.FileLine(pc)
// The file is something like /a/b/c/d.go. We want just the d.
if strings.HasSuffix(file, ".go") {
file = file[:len(file)-3]
}
file = strings.TrimSuffix(file, ".go")
if slash := strings.LastIndex(file, "/"); slash >= 0 {
file = file[slash+1:]
}
Expand Down
Loading
Loading