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

Fix some golangci-lint errors #42405

Merged
merged 4 commits into from
Jan 24, 2025
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
13 changes: 7 additions & 6 deletions filebeat/registrar/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (r *Registrar) Start() error {
// Load the previous log file locations now, for use in input
err := r.loadStates()
if err != nil {
return fmt.Errorf("error loading state: %v", err)
return fmt.Errorf("error loading state: %w", err)
}

r.wg.Add(1)
Expand Down Expand Up @@ -141,7 +141,9 @@ func (r *Registrar) Run() {
defer r.store.Close()

defer func() {
writeStates(r.store, r.states.GetStates())
if err := writeStates(r.store, r.states.GetStates()); err != nil {
r.log.Errorf("Error writing stopping registrar state to statestore: %v", err)
}
}()

var (
Expand Down Expand Up @@ -242,8 +244,7 @@ func (r *Registrar) gcStates() {

beforeCount := r.states.Count()
cleanedStates, pendingClean := r.states.CleanupWith(func(id string) {
// TODO: report error
r.store.Remove(fileStatePrefix + id)
r.store.Remove(fileStatePrefix + id) //nolint:errcheck // TODO: report error
})
statesCleanup.Add(int64(cleanedStates))

Expand Down Expand Up @@ -274,13 +275,13 @@ func readStatesFrom(store *statestore.Store) ([]file.State, error) {
return true, nil
}

// try to decode. Ingore faulty/incompatible values.
// try to decode. Ignore faulty/incompatible values.
var st file.State
if err := dec.Decode(&st); err != nil {
// XXX: Do we want to log here? In case we start to store other
// state types in the registry, then this operation will likely fail
// quite often, producing some false-positives in the logs...
return true, nil
return true, nil //nolint:nilerr // Ignore per comment above
}

st.Id = key[len(fileStatePrefix):]
Expand Down
4 changes: 2 additions & 2 deletions libbeat/common/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//
// Use the helpers with `defer`. For example use IfNot with `defer`, such that
// cleanup functions will be executed if `check` is false, no matter if an
// error has been returned or an panic has occured.
// error has been returned or an panic has occurred.
//
// initOK := false
// defer cleanup.IfNot(&initOK, func() {
Expand Down Expand Up @@ -59,7 +59,7 @@ func IfNotPred(pred func() bool, cleanup func()) {
}
}

// WithError returns a cleanup function calling a custom handler if an error occured.
// WithError returns a cleanup function calling a custom handler if an error occurred.
func WithError(fn func(error), cleanup func() error) func() {
return func() {
if err := cleanup(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion libbeat/common/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestSchemaCases(t *testing.T) {
if errs != nil {
errorMessage := errs.Error()
if tc.expectedErrorMessage == "" {
t.Errorf("unexpected error ocurred: %s", errorMessage)
t.Errorf("unexpected error occurred: %s", errorMessage)
}
assert.Contains(t, errorMessage, tc.expectedErrorMessage)
} else if tc.expectedErrorMessage != "" {
Expand Down
7 changes: 2 additions & 5 deletions libbeat/statestore/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
)

// ErrorAccess indicates that an error occured when trying to open a Store.
// ErrorAccess indicates that an error occurred when trying to open a Store.
type ErrorAccess struct {
name string
cause error
Expand Down Expand Up @@ -84,8 +84,5 @@ func (e *ErrorOperation) Error() string {
// IsClosed returns true if the cause for an Error is ErrorClosed.
func IsClosed(err error) bool {
var tmp *ErrorClosed
if errors.As(err, &tmp) {
return true
}
return false
return errors.As(err, &tmp)
}
14 changes: 7 additions & 7 deletions libbeat/statestore/internal/storecompliance/reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// Registry helper for writing tests.
// The registry uses a testing.T and provides some MustX methods that fail if
// an error occured.
// an error occurred.
type Registry struct {
T testing.TB
backend.Registry
Expand All @@ -34,7 +34,7 @@ type Registry struct {
// Store helper for writing tests.
// The store needs a reference to the Registry with the current test context.
// The Store provides additional helpers for reopening the store, MustX methods
// that will fail the test if an error has occured.
// that will fail the test if an error has occurred.
type Store struct {
backend.Store

Expand All @@ -51,7 +51,7 @@ func (r *Registry) Access(name string) (*Store, error) {
return &Store{Store: s, Registry: r, name: name}, nil
}

// MustAccess opens a Store. It fails the test if an error has occured.
// MustAccess opens a Store. It fails the test if an error has occurred.
func (r *Registry) MustAccess(name string) *Store {
store, err := r.Access(name)
must(r.T, err, "open store")
Expand Down Expand Up @@ -87,26 +87,26 @@ func (s *Store) Reopen() {
s.Store = store
}

// MustHave fails the test if an error occured in a call to Has.
// MustHave fails the test if an error occurred in a call to Has.
func (s *Store) MustHave(key string) bool {
b, err := s.Has(key)
must(s.Registry.T, err, "unexpected error on store/has call")
return b
}

// MustGet fails the test if an error occured in a call to Get.
// MustGet fails the test if an error occurred in a call to Get.
func (s *Store) MustGet(key string, into interface{}) {
err := s.Get(key, into)
must(s.Registry.T, err, "unexpected error on store/get call")
}

// MustSet fails the test if an error occured in a call to Set.
// MustSet fails the test if an error occurred in a call to Set.
func (s *Store) MustSet(key string, from interface{}) {
err := s.Set(key, from)
must(s.Registry.T, err, "unexpected error on store/set call")
}

// MustRemove fails the test if an error occured in a call to Remove.
// MustRemove fails the test if an error occurred in a call to Remove.
func (s *Store) MustRemove(key string) {
err := s.Store.Remove(key)
must(s.Registry.T, err, "unexpected error remove key")
Expand Down
8 changes: 7 additions & 1 deletion libbeat/statestore/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package statestore

import (
"fmt"

"github.com/stretchr/testify/mock"

"github.com/elastic/beats/v7/libbeat/statestore/backend"
Expand All @@ -39,7 +41,11 @@ func (m *mockRegistry) Access(name string) (backend.Store, error) {

var store backend.Store
if ifc := args.Get(0); ifc != nil {
store = ifc.(backend.Store)
var ok bool
store, ok = ifc.(backend.Store)
if !ok {
return store, fmt.Errorf("unexpected type: %T", store)
}
}

return store, args.Error(1)
Expand Down
2 changes: 1 addition & 1 deletion libbeat/statestore/storetest/storetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *MapStore) Has(key string) (bool, error) {
}

// Get returns a key value pair from the store. An error is returned if the
// store has been closed, the key is unknown, or an decoding error occured.
// store has been closed, the key is unknown, or an decoding error occurred.
func (s *MapStore) Get(key string, into interface{}) error {
s.mu.RLock()
defer s.mu.RUnlock()
Expand Down
Loading