Skip to content

Commit

Permalink
Fix linter errors (#5442)
Browse files Browse the repository at this point in the history
* Remove unused symbols

* Fix bodycloser linter errors

* Fix errcheck linter errors

* Fix staticcheck linter errors

* Use default configuration for nakedret linter

* Fix various linter errors
  • Loading branch information
swiatekm authored Oct 1, 2024
1 parent 218c984 commit a9d54fb
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 115 deletions.
4 changes: 0 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ linters-settings:
# Select the Go version to target. The default is '1.13'.
go: "1.22.7"

nakedret:
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
max-func-lines: 0

nolintlint:
# Enable to ensure that nolint directives are all used. Default is true.
allow-unused: false
Expand Down
13 changes: 10 additions & 3 deletions dev-tools/cmd/buildfleetcfg/buildfleetcfg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions dev-tools/licenses/license_generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dev-tools/mage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// It checks the file permissions of python test cases and YAML files.
// It checks .go source files using 'go vet'.
func Check() error {
fmt.Println(">> check: Checking source code for common problems") //nolint:forbidigo // it's ok to use fmt.println in mage
fmt.Println(">> check: Checking source code for common problems")

mg.Deps(GoVet, CheckYAMLNotExecutable, devtools.CheckNoChanges)

Expand Down Expand Up @@ -69,7 +69,7 @@ func GoVet() error {

// CheckLicenseHeaders checks license headers in .go files.
func CheckLicenseHeaders() error {
fmt.Println(">> fmt - go-licenser: Checking for missing headers") //nolint:forbidigo // it's ok to use fmt.println in mage
fmt.Println(">> fmt - go-licenser: Checking for missing headers")
mg.Deps(InstallGoLicenser)

licenser := gotool.Licenser
Expand Down
4 changes: 0 additions & 4 deletions dev-tools/mage/gotool/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ func runVGo(cmd string, args *Args) error {
}, cmd, args)
}

func runGo(cmd string, args *Args) error {
return execGoWith(sh.RunWith, cmd, args)
}

func execGoWith(
fn func(map[string]string, string, ...string) error,
cmd string, args *Args,
Expand Down
20 changes: 0 additions & 20 deletions dev-tools/mage/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,3 @@ func kubectlIn(env map[string]string, stdout, stderr io.Writer, input string, ar

return c.Run()
}

func kubectlStart(env map[string]string, stdout, stderr io.Writer, args ...string) (*exec.Cmd, error) {
c := exec.Command("kubectl", args...)
c.Env = os.Environ()
for k, v := range env {
c.Env = append(c.Env, k+"="+v)
}
c.Stdout = stdout
c.Stderr = stderr
c.Stdin = nil

if mg.Verbose() {
fmt.Println("exec:", "kubectl", strings.Join(args, " "))
}

if err := c.Start(); err != nil {
return nil, err
}
return c, nil
}
6 changes: 5 additions & 1 deletion dev-tools/mage/target/common/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func PackageSystemTests() error {
parent := filepath.Dir(targetFile)
if !fileExists(parent) {
fmt.Printf(">> creating parent dir: %s", parent)
os.Mkdir(parent, 0750)
err = os.Mkdir(parent, 0750)
if err != nil {
fmt.Printf(">> %s", err)
return err
}
}

err = devtools.Tar(systemTestsDir, targetFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestExpectedCloudProcessID(t *testing.T) {
}

for _, tc := range testcases {
tc := tc // make a copy to avoid implicit memory aliasing
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.id, expectedCloudProcessID(&tc.component))
})
Expand Down Expand Up @@ -142,6 +143,7 @@ func TestMatchesCloudProcessID(t *testing.T) {
}

for _, tc := range testcases {
tc := tc // make a copy to avoid implicit memory aliasing
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.matches, matchesCloudProcessID(&tc.component, tc.processID))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAddingHeaders(t *testing.T) {
rtt := WithHeaders(c.Transport, Headers)

c.Transport = rtt
resp, err := c.Get(server.URL)
resp, err := c.Get(server.URL) //nolint:noctx // this is fine in tests
require.NoError(t, err)
b, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/agent/errors/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func TestErrorsWrap(t *testing.T) {
ew := fmt.Errorf("wrapper: %w", ce)
outer := New(ew)

outerCustom, ok := outer.(Error)
var outerCustom Error
ok := errors.As(outer, &outerCustom)
if !ok {
t.Error("expected Error")
return
Expand Down Expand Up @@ -109,7 +110,8 @@ func TestErrors(t *testing.T) {

for _, tc := range cases {
actualErr := New(tc.args...)
agentErr, ok := actualErr.(Error)
var agentErr Error
ok := errors.As(actualErr, &agentErr)
if !ok {
t.Errorf("[%s] expected Error", tc.id)
continue
Expand Down Expand Up @@ -151,7 +153,8 @@ func TestMetaFold(t *testing.T) {
err3 := New("level3", err2, M("key1", "level3"), M("key3", "level3"))
err4 := New("level4", err3)

resultingErr, ok := err4.(Error)
var resultingErr Error
ok := errors.As(err4, &resultingErr)
if !ok {
t.Fatal("error is not Error")
}
Expand Down Expand Up @@ -186,7 +189,8 @@ func TestMetaCallDoesNotModifyCollection(t *testing.T) {
err3 := New("level3", err2, M("key1", "level3"), M("key3", "level3"))
err4 := New("level4", err3)

resultingErr, ok := err4.(agentError)
var resultingErr agentError
ok := errors.As(err4, &resultingErr)
if !ok {
t.Fatal("error is not Error")
}
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/fleetapi/uploader/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ func Test_retrySender_Send(t *testing.T) {
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.status, resp.StatusCode)
}
defer func() {
if resp != nil && resp.Body != nil {
err := resp.Body.Close()
assert.NoError(t, err)
}
}()
sender.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -166,6 +172,12 @@ func Test_retrySender_bodyValidation(t *testing.T) {
wait: backoff,
}
resp, err := c.Send(context.Background(), "POST", "/", nil, nil, bytes.NewReader([]byte("abcd")))
defer func() {
if resp != nil && resp.Body != nil {
err := resp.Body.Close()
assert.NoError(t, err)
}
}()
require.NoError(t, err)
assert.Equal(t, resp.StatusCode, 200)
assert.Equal(t, []byte("abcd"), body1)
Expand Down
26 changes: 0 additions & 26 deletions pkg/component/fake/component/comp/dialer.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/component/fake/component/comp/dialer_windows.go

This file was deleted.

30 changes: 14 additions & 16 deletions pkg/component/runtime/service_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,22 @@ func executeCommand(ctx context.Context, log *logger.Logger, binaryPath string,
// channel for the last error message from the stderr output
errch := make(chan string, 1)
ctxStderr := contextio.NewReader(ctx, proc.Stderr)
if ctxStderr != nil {
go func() {
var errText string
scanner := bufio.NewScanner(ctxStderr)
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 {
txt := strings.TrimSpace(line)
if len(txt) > 0 {
errText = txt
// Log error output line
log.Error(errText)
}
go func() {
var errText string
scanner := bufio.NewScanner(ctxStderr)
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 {
txt := strings.TrimSpace(line)
if len(txt) > 0 {
errText = txt
// Log error output line
log.Error(errText)
}
}
errch <- errText
}()
}
}
errch <- errText
}()

procState := <-proc.Wait()
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/control/v1/client/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func dialContext(ctx context.Context) (*grpc.ClientConn, error) {
return grpc.DialContext(
return grpc.DialContext( //nolint:staticcheck // Only the deprecated version allows this call to be blocking
ctx,
strings.TrimPrefix(control.Address(), "unix://"),
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
2 changes: 1 addition & 1 deletion pkg/control/v1/client/dial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func dialContext(ctx context.Context) (*grpc.ClientConn, error) {
return grpc.DialContext(
return grpc.DialContext( //nolint:staticcheck // Only the deprecated version allows this call to be blocking
ctx,
control.Address(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
2 changes: 1 addition & 1 deletion pkg/control/v2/client/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr
grpc.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),
)
return grpc.DialContext(ctx, address, opts...)
return grpc.DialContext(ctx, address, opts...) //nolint:staticcheck // Only the deprecated version allows this call to be blocking
}

func dialer(ctx context.Context, addr string) (net.Conn, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/control/v2/client/dial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr
grpc.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),
)
return grpc.DialContext(ctx, address, opts...)
return grpc.DialContext(ctx, address, opts...) //nolint:staticcheck // Only the deprecated version allows this call to be blocking
}

func dialer(ctx context.Context, addr string) (net.Conn, error) {
Expand Down
2 changes: 1 addition & 1 deletion testing/upgradetest/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func WaitHealthyAndVersion(ctx context.Context, f *atesting.Fixture, versionInfo
// If we're in an upgrade process, the versions might not match
// so we wait to see if we get to a stable version
if errors.Is(err, ErrVerMismatch) {
logger.Logf("version mismatch, ignoring it, time until timeout: %s", deadline.Sub(time.Now()))
logger.Logf("version mismatch, ignoring it, time until timeout: %s", time.Until(deadline))
continue
}
if err == nil {
Expand Down

0 comments on commit a9d54fb

Please sign in to comment.