Skip to content

Commit

Permalink
golangci-lint: Add more linters (#2877)
Browse files Browse the repository at this point in the history
Adds new 1.63 linters:
- https://github.com/alingse/nilnesserr: Provides a concise way to
  detect return an unrelated/nil-values error.
- https://github.com/ldez/exptostd: Detects functions from
  golang.org/x/exp/ that can be replaced by std functions.
- https://github.com/ldez/usetesting: Detects when some calls can be
  replaced by methods from the testing package.

Additionally, add more older, useful linters.
  • Loading branch information
orestisfl authored Jan 3, 2025
1 parent b0f4341 commit e4e4996
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 152 deletions.
13 changes: 12 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,37 @@ run:

linters:
enable:
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- exhaustruct
- copyloopvar
- errname
- exhaustruct
- exptostd
- fatcontext
- gci
- gocritic
- gocyclo
- gofmt
- gomodguard
- iface
- makezero
- misspell
- nilerr
- nilnesserr
- nolintlint
- prealloc
- predeclared
- reassign
- recvcheck
- revive
- testifylint
- unconvert
- unparam
- unused
- usetesting
- wastedassign
- whitespace

issues:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/golangci-lint
2 changes: 1 addition & 1 deletion internal/inventory/azurefetcher/fetcher_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (f *storageFetcher) listStorageAccounts(ctx context.Context) ([]azurelib.Az
return nil, fmt.Errorf("error listing subscriptions: %v", err)
}

subscriptionIds := make([]string, len(subscriptions))
subscriptionIds := make([]string, 0, len(subscriptions))
for _, subscription := range subscriptions {
subscriptionIds = append(subscriptionIds, subscription.Name)
}
Expand Down
28 changes: 14 additions & 14 deletions internal/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ const (
shutdownGracePeriod = 20 * time.Second
)

// ErrorStopSignal is used to indicate we got a stop signal
var ErrorStopSignal = errors.New("stop beater")
// ErrStopSignal is used to indicate we got a stop signal
var ErrStopSignal = errors.New("stop beater")

// ErrorGracefulExit is used when the launcher stops before shutdownGracePeriod, after waiting for the beater to stop
var ErrorGracefulExit = beat.GracefulExit
// ErrGracefulExit is used when the launcher stops before shutdownGracePeriod, after waiting for the beater to stop
var ErrGracefulExit = beat.GracefulExit

// ErrorTimeoutExit is used when the launcher stops after shutdownGracePeriod, without waiting for the beater to stop
var ErrorTimeoutExit = errors.New("exit after timeout")
// ErrTimeoutExit is used when the launcher stops after shutdownGracePeriod, without waiting for the beater to stop
var ErrTimeoutExit = errors.New("exit after timeout")

type launcher struct {
wg sync.WaitGroup // WaitGroup used to wait for active beaters
Expand Down Expand Up @@ -115,9 +115,9 @@ func (l *launcher) run() error {
err := l.runLoop()

switch {
case errors.Is(err, ErrorGracefulExit):
case errors.Is(err, ErrGracefulExit):
l.log.Info("Launcher stopped successfully")
case errors.Is(err, ErrorTimeoutExit):
case errors.Is(err, ErrTimeoutExit):
l.log.Info("Launcher stopped after timeout")
case err == nil: // unexpected
default:
Expand All @@ -140,7 +140,7 @@ func (l *launcher) runLoop() error {

// Wait for something to happen:
// config update (val, nil)
// stop signal (nil, ErrorStopSignal)
// stop signal (nil, ErrStopSignal)
// beater error (nil, err)
cfg, err := l.waitForUpdates()

Expand Down Expand Up @@ -224,24 +224,24 @@ func (l *launcher) stopBeaterWithTimeout(duration time.Duration) error {
select {
case <-time.After(duration):
l.log.Infof("Grace period for %s ended", l.name)
return ErrorTimeoutExit
return ErrTimeoutExit
case <-wgCh:
l.log.Infof("Launcher shut %s down gracefully", l.name)
return ErrorGracefulExit
return ErrGracefulExit
}
}

// waitForUpdates is the function that keeps Launcher runLoop busy.
// It will finish for one of following reasons:
// 1. The Stop function got called (nil, ErrorStopSignal)
// 1. The Stop function got called (nil, ErrStopSignal)
// 2. The beater run has returned (nil, err)
// 3. A config update received (val, nil)
func (l *launcher) waitForUpdates() (*config.C, error) {
select {
case err, ok := <-l.beaterErr:
if !ok {
l.log.Infof("Launcher received a stop signal")
return nil, ErrorStopSignal
return nil, ErrStopSignal
}
return nil, err

Expand All @@ -260,7 +260,7 @@ func isConfigUpdate(cfg *config.C, err error) bool {
}

func isStopSignal(cfg *config.C, err error) bool {
return cfg == nil && errors.Is(err, ErrorStopSignal)
return cfg == nil && errors.Is(err, ErrStopSignal)
}

func isBeaterError(cfg *config.C, err error) bool {
Expand Down
10 changes: 5 additions & 5 deletions internal/launcher/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (s *LauncherTestSuite) TestWaitForUpdates() {
}(tt.configs)

err := sut.run()
s.Require().ErrorIs(err, ErrorGracefulExit)
s.Require().ErrorIs(err, ErrGracefulExit)
beater, ok := sut.beater.(*beaterMock)
s.Require().True(ok)
s.Equal(tt.expected, beater.cfg)
Expand Down Expand Up @@ -450,7 +450,7 @@ func (s *LauncherTestSuite) TestLauncherUpdateAndStop() {
sut.Stop()
}()
err := sut.run()
s.Require().ErrorIs(err, ErrorGracefulExit)
s.Require().ErrorIs(err, ErrGracefulExit)
}

func (s *LauncherTestSuite) TestLauncherStopTwicePanics() {
Expand All @@ -461,7 +461,7 @@ func (s *LauncherTestSuite) TestLauncherStopTwicePanics() {
sut.Stop()
}()
err := sut.run()
s.Require().ErrorIs(err, ErrorGracefulExit)
s.Require().ErrorIs(err, ErrGracefulExit)

s.Panics(func() {
sut.Stop()
Expand All @@ -481,7 +481,7 @@ func (s *LauncherTestSuite) TestLauncherStop() {
}()

err := sut.run()
s.Require().ErrorIs(err, ErrorGracefulExit)
s.Require().ErrorIs(err, ErrGracefulExit)
}

func (s *LauncherTestSuite) TestLauncherStopTimeout() {
Expand All @@ -496,7 +496,7 @@ func (s *LauncherTestSuite) TestLauncherStopTimeout() {
}()

err := sut.run()
s.Require().ErrorIs(err, ErrorTimeoutExit)
s.Require().ErrorIs(err, ErrTimeoutExit)
}

func (s *LauncherTestSuite) initMocks() *launcherMocks {
Expand Down
20 changes: 10 additions & 10 deletions internal/processor/add_cluster_id/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ import (
)

type (
errConfigUnpack struct{ cause error }
errComputeID struct{ cause error }
configUnpackError struct{ cause error }
computeIDError struct{ cause error }
)

func makeErrConfigUnpack(cause error) errConfigUnpack {
return errConfigUnpack{cause}
func makeErrConfigUnpack(cause error) configUnpackError {
return configUnpackError{cause}
}
func (e errConfigUnpack) Error() string {
func (e configUnpackError) Error() string {
return fmt.Sprintf("failed to unpack %v processor configuration: %v", processorName, e.cause)
}
func (e errConfigUnpack) Unwrap() error {
func (e configUnpackError) Unwrap() error {
return e.cause
}

func makeErrComputeID(cause error) errComputeID {
return errComputeID{cause}
func makeErrComputeID(cause error) computeIDError {
return computeIDError{cause}
}
func (e errComputeID) Error() string {
func (e computeIDError) Error() string {
return fmt.Sprintf("failed to compute ID: %v", e.cause)
}
func (e errComputeID) Unwrap() error {
func (e computeIDError) Unwrap() error {
return e.cause
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var successfulCurrentCloudRegionOutput = &ec2imds.InstanceIdentityDocument{
type CurrentRegionSelectorTestSuite struct {
suite.Suite
selector *currentRegionSelector
mock *mockCurrentCloudRegion
mock *MockMetadataProvider
}

func TestCurrentRegionSelectorTestSuite(t *testing.T) {
Expand All @@ -46,7 +46,7 @@ func TestCurrentRegionSelectorTestSuite(t *testing.T) {

func (s *CurrentRegionSelectorTestSuite) SetupTest() {
s.selector = &currentRegionSelector{}
s.mock = &mockCurrentCloudRegion{}
s.mock = &MockMetadataProvider{}
s.selector.client = s.mock
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
ec2imds "github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/elastic/elastic-agent-libs/logp"
)

type currentRegionSelector struct {
client currentCloudRegion
}

type currentCloudRegion interface {
GetMetadata(ctx context.Context, cfg aws.Config) (*ec2imds.InstanceIdentityDocument, error)
client MetadataProvider
}

func (s *currentRegionSelector) Regions(ctx context.Context, cfg aws.Config) ([]string, error) {
Expand Down
112 changes: 0 additions & 112 deletions internal/resources/providers/awslib/mock_current_cloud_region.go

This file was deleted.

3 changes: 3 additions & 0 deletions internal/resources/utils/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func matchGroupIndexValue(value string, idx int) lineFunc {
return
}
if _, err := strconv.Atoi(parts[2]); err != nil {
//nolint:nilerr
return nil, nil
}
return &Group{Name: parts[0], Gid: parts[2]}, nil
Expand Down Expand Up @@ -240,9 +241,11 @@ func matchUserIndexValue(value string, idx int) lineFunc {
return
}
if _, err := strconv.Atoi(parts[2]); err != nil {
//nolint:nilerr
return nil, nil
}
if _, err := strconv.Atoi(parts[3]); err != nil {
//nolint:nilerr
return nil, nil
}
u := &User{
Expand Down

0 comments on commit e4e4996

Please sign in to comment.