Skip to content

Commit

Permalink
lint: fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kruskall committed Sep 29, 2024
1 parent 8d389db commit 6053d69
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
4 changes: 3 additions & 1 deletion dev-tools/mage/integtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ func (r *IntegrationRunner) Test(mageTarget string, test func() error) (err erro
inTeardown := false
defer func() {
if recoverErr := recover(); recoverErr != nil {
err = recoverErr.(error)
if rerr, ok := recoverErr.(error); ok {
err = rerr
}
if !inTeardown {
// ignore errors
_ = r.steps.Teardown(r.env)
Expand Down
39 changes: 22 additions & 17 deletions libbeat/common/schema/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package schema

import (
"errors"

"github.com/elastic/elastic-agent-libs/mapstr"
)

Expand All @@ -29,46 +31,49 @@ type ApplyOption func(mapstr.M, []error) (mapstr.M, []error)

// AllRequired considers any missing field as an error, except if explicitly
// set as optional
func AllRequired(event mapstr.M, errors []error) (mapstr.M, []error) {
func AllRequired(event mapstr.M, errs []error) (mapstr.M, []error) {
k := 0
for i, err := range errors {
if err, ok := err.(*KeyNotFoundError); ok {
if err.Optional {
for i, err := range errs {
var keyErr *KeyNotFoundError
if errors.As(err, &keyErr) {
if keyErr.Optional {
continue
}
}
errors[k] = errors[i]
errs[k] = errs[i]
k++
}
return event, errors[:k]
return event, errs[:k]
}

// FailOnRequired considers missing fields as an error only if they are set
// as required
func FailOnRequired(event mapstr.M, errors []error) (mapstr.M, []error) {
func FailOnRequired(event mapstr.M, errs []error) (mapstr.M, []error) {
k := 0
for i, err := range errors {
if err, ok := err.(*KeyNotFoundError); ok {
if !err.Required {
for i, err := range errs {
var keyErr *KeyNotFoundError
if errors.As(err, &keyErr) {
if !keyErr.Required {
continue
}
}
errors[k] = errors[i]
errs[k] = errs[i]
k++
}
return event, errors[:k]
return event, errs[:k]
}

// NotFoundKeys calls a function with the list of missing keys as parameter
func NotFoundKeys(cb func(keys []string)) ApplyOption {
return func(event mapstr.M, errors []error) (mapstr.M, []error) {
return func(event mapstr.M, errs []error) (mapstr.M, []error) {
var keys []string
for _, err := range errors {
if err, ok := err.(*KeyNotFoundError); ok {
keys = append(keys, err.Key())
for _, err := range errs {
var keyErr *KeyNotFoundError
if errors.As(err, &keyErr) {
keys = append(keys, keyErr.Key())
}
}
cb(keys)
return event, errors
return event, errs
}
}
11 changes: 9 additions & 2 deletions metricbeat/module/elasticsearch/index/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,15 @@ func getIndexStatus(shards map[string]interface{}) (string, error) {

shard := mapstr.M(s)

isPrimary := shard["primary"].(bool)
state := shard["state"].(string)
isPrimary, ok := shard["primary"].(bool)
if !ok {
return "", fmt.Errorf("%v.shards[primary] is not a bool", indexName)
}

state, ok := shard["state"].(string)
if !ok {
return "", fmt.Errorf("%v.shards[state] is not a string", indexName)
}

if isPrimary {
areAllPrimariesStarted = areAllPrimariesStarted && (state == "STARTED")
Expand Down
5 changes: 4 additions & 1 deletion x-pack/auditbeat/module/system/socket/helper/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func NewIPv6Loopback() (lo IPv6Loopback, err error) {
func (lo *IPv6Loopback) AddRandomAddress() (addr net.IP, err error) {
addr = make(net.IP, 16)
addr[0] = 0xFD
rand.Read(addr[1:])
_, err = rand.Read(addr[1:])
if err != nil {
return nil, fmt.Errorf("rand.Read failed: %w", err)
}
var req in6Ifreq
copy(req.addr[:], addr)
req.ifindex = lo.ifreq.index
Expand Down

0 comments on commit 6053d69

Please sign in to comment.