Skip to content

Commit

Permalink
Add staticcheck github action (#4489)
Browse files Browse the repository at this point in the history
* Add staticcheck github action

Signed-off-by: Eric Chlebek <[email protected]>

* Tweak

Signed-off-by: Eric Chlebek <[email protected]>

* add staticcheck.conf, remove golangci-lint

Signed-off-by: Justin Kolberg <[email protected]>

* remove duplicate imports

Signed-off-by: Justin Kolberg <[email protected]>

* add staticcheck & fix failing checks

Signed-off-by: Justin Kolberg <[email protected]>

Co-authored-by: Justin Kolberg <[email protected]>
  • Loading branch information
echlebek and amdprophet authored Feb 8, 2022
1 parent 859b27a commit 2e20ead
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 143 deletions.
89 changes: 0 additions & 89 deletions .github/workflows/golangci-lint.yml

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/static-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: static-check

on:
push:
tags:
- 'v*'
branches:
- main
pull_request:
branches:
- '*'

jobs:
staticcheck:
name: staticcheck (project)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: dominikh/[email protected]
with:
version: "2021.1.1"
env:
GO_VERSION: 1.17.1
4 changes: 0 additions & 4 deletions .golangci.yml

This file was deleted.

7 changes: 5 additions & 2 deletions agent/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"errors"
)

// StatsdUnsupported is returned when statsd can't be supported on the platform.
var StatsdUnsupported = errors.New("statsd not supported on this platform")
// ErrStatsdUnsupported is returned when statsd can't be supported on the platform.
var ErrStatsdUnsupported = errors.New("statsd not supported on this platform")

// DEPRECATED: use ErrStatsdUnsupported
var StatsdUnsupported = ErrStatsdUnsupported

// StatsdServer is the interface the agent requires to run a statsd server.
type StatsdServer interface {
Expand Down
5 changes: 2 additions & 3 deletions agent/statsd_server_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ func GetMetricsAddr(s StatsdServer) string {

// statsdServer is a no-op statsd server for solaris support.
// the gostatsd package requires a library that can't be built on solaris.
type statsdServer struct {
}
type statsdServer struct{}

func (s statsdServer) Run(context.Context) error {
return StatsdUnsupported
return ErrStatsdUnsupported
}

func NewStatsdServer(*Agent) statsdServer {
Expand Down
5 changes: 2 additions & 3 deletions backend/apid/graphql/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/graphql-go/graphql"
corev2 "github.com/sensu/sensu-go/api/core/v2"
v2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/apid/graphql/globalid"
"github.com/sensu/sensu-go/backend/apid/graphql/schema"
"github.com/sensu/sensu-go/types"
Expand Down Expand Up @@ -91,7 +90,7 @@ func checkIsSilencedBy(check interface {

// ToJSON implements response to request for 'toJSON' field.
func (r *checkCfgImpl) ToJSON(p graphql.ResolveParams) (interface{}, error) {
return types.WrapResource(p.Source.(v2.Resource)), nil
return types.WrapResource(p.Source.(corev2.Resource)), nil
}

// RuntimeAssets implements response to request for 'runtimeAssets' field.
Expand Down Expand Up @@ -226,7 +225,7 @@ func (r *checkImpl) RuntimeAssets(p graphql.ResolveParams) (interface{}, error)

// ToJSON implements response to request for 'toJSON' field.
func (r *checkImpl) ToJSON(p graphql.ResolveParams) (interface{}, error) {
return types.WrapResource(p.Source.(v2.Resource)), nil
return types.WrapResource(p.Source.(corev2.Resource)), nil
}

//
Expand Down
3 changes: 1 addition & 2 deletions backend/apid/graphql/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

corev2 "github.com/sensu/sensu-go/api/core/v2"
v2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/apid/graphql/schema"
"github.com/sensu/sensu-go/graphql"
"github.com/stretchr/testify/assert"
Expand All @@ -22,7 +21,7 @@ func TestEntityTypeMetadataField(t *testing.T) {
res, err := impl.Metadata(graphql.ResolveParams{Source: src, Context: context.Background()})
require.NoError(t, err)
assert.NotEmpty(t, res)
assert.IsType(t, v2.ObjectMeta{}, res)
assert.IsType(t, corev2.ObjectMeta{}, res)
}

func TestEntityTypeRelatedField(t *testing.T) {
Expand Down
16 changes: 10 additions & 6 deletions backend/apid/graphql/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import (
)

var (
// KeylessStatementErr statement is missing a key
KeylessStatementErr = errors.New("filters must have the format KEY:VAL")
// ErrKeylessStatement is returned when a statement is missing a key
ErrKeylessStatement = errors.New("filters must have the format KEY:VAL")
// DEPRECATED: use ErrKeylessStatement
KeylessStatementErr = ErrKeylessStatement

// FilterNotFoundErr could not match a filter for the given key
FilterNotFoundErr = errors.New("no filter could be matched with the given statement")
// ErrFilterNotFound is returned when a filter is not found for a given key
ErrFilterNotFound = errors.New("no filter could be matched with the given statement")
// DEPRECATED: use ErrFilterNotFound
FilterNotFoundErr = ErrFilterNotFound
)

// Match a given resource
Expand All @@ -35,12 +39,12 @@ func Compile(statements []string, filters map[string]Filter, fieldsFn FieldsFunc
for _, s := range statements {
ss := strings.SplitN(s, statementSeparator, 2)
if len(ss) != 2 {
return nil, KeylessStatementErr
return nil, ErrKeylessStatement
}
k, v := ss[0], ss[1]
f, ok := filters[k]
if !ok {
return nil, FilterNotFoundErr
return nil, ErrFilterNotFound
}
matcher, err := f(v, fieldsFn)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/apid/graphql/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCompile(t *testing.T) {

// statement doesn't match a filter
m, err = Compile([]string{"unknown:test"}, filters, nil)
require.Error(t, err, KeylessStatementErr)
require.Error(t, err, ErrKeylessStatement)
assert.Nil(t, m)

// statement is not valid
Expand Down
3 changes: 1 addition & 2 deletions backend/apid/graphql/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"testing"

corev2 "github.com/sensu/sensu-go/api/core/v2"
v2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/graphql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMutatorTypeToJSONField(t *testing.T) {
src := v2.FixtureMutator("name")
src := corev2.FixtureMutator("name")
imp := &mutatorImpl{}

res, err := imp.ToJSON(graphql.ResolveParams{Source: src, Context: context.Background()})
Expand Down
1 change: 1 addition & 0 deletions backend/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func InitCommand() *cobra.Command {
return err
}
if opts.AdminPassword != opts.AdminPasswordConfirmation {
//lint:ignore ST1005 this error is written to stdout/stderr
return errors.New("Password confirmation doesn't match the password")
}
initConfig.SeedConfig.AdminUsername = opts.AdminUsername
Expand Down
3 changes: 1 addition & 2 deletions backend/keepalived/keepalived_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/sensu/sensu-go/backend/liveness"
"github.com/sensu/sensu-go/backend/messaging"
stor "github.com/sensu/sensu-go/backend/store"
storev2 "github.com/sensu/sensu-go/backend/store/v2"
storv2 "github.com/sensu/sensu-go/backend/store/v2"
"github.com/sensu/sensu-go/backend/store/v2/storetest"
"github.com/sensu/sensu-go/backend/store/v2/wrap"
Expand Down Expand Up @@ -429,7 +428,7 @@ func TestDeadCallbackNoEvent(t *testing.T) {
t.Fatal(err)
}

wrapper, err := storev2.WrapResource(
wrapper, err := storv2.WrapResource(
corev3.FixtureEntityConfig("entity1"),
[]wrap.Option{wrap.CompressNone, wrap.EncodeJSON}...)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions backend/pipeline/filter/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
"not_silenced",
}

getFilterErr = errors.New("could not retrieve filter")
errCouldNotRetrieveFilter = errors.New("could not retrieve filter")

// PipelineFilterFuncs gets patched by enterprise sensu-go
PipelineFilterFuncs map[string]interface{}
Expand Down Expand Up @@ -77,12 +77,12 @@ func (l *LegacyAdapter) Filter(ctx context.Context, ref *corev2.ResourceReferenc
filter, err := l.Store.GetEventFilterByName(tctx, ref.Name)
cancel()
if err != nil {
logger.WithFields(fields).WithError(err).Warning(getFilterErr.Error())
logger.WithFields(fields).WithError(err).Warning(errCouldNotRetrieveFilter.Error())
return false, err
}
if filter == nil {
logger.WithFields(fields).WithError(err).Warning(getFilterErr.Error())
return false, fmt.Errorf(getFilterErr.Error())
logger.WithFields(fields).WithError(err).Warning(errCouldNotRetrieveFilter.Error())
return false, fmt.Errorf(errCouldNotRetrieveFilter.Error())
}

// Execute the filter, evaluating each of its
Expand Down
6 changes: 3 additions & 3 deletions backend/pipeline/mutator/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
)

var (
halt = errors.New("halt")
errHalt = errors.New("halt")
)

// JavascriptAdapter is a mutator adapter which mutates an event using
Expand Down Expand Up @@ -146,7 +146,7 @@ func (m *MutatorExecutionEnvironment) Eval(ctx context.Context, expression strin
}
vm.Interrupt = make(chan func(), 1)
defer func() {
if e := recover(); e != nil && e == halt {
if e := recover(); e != nil && e == errHalt {
err = errors.New("mutator timeout reached, execution halted")
} else if e != nil {
panic(e)
Expand All @@ -158,7 +158,7 @@ func (m *MutatorExecutionEnvironment) Eval(ctx context.Context, expression strin
select {
case <-time.After(m.Timeout):
vm.Interrupt <- func() {
panic(halt)
panic(errHalt)
}
case <-done:
}
Expand Down
2 changes: 0 additions & 2 deletions backend/store/etag.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ func scanETag(header string) (string, string) {
case c == 0x21 || c >= 0x23 && c <= 0x7E || c >= 0x80:
case c == '"':
return string(header[:i+1]), header[i+1:]
default:
break
}
}

Expand Down
23 changes: 11 additions & 12 deletions backend/store/etcd/namespace_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/backend/store/etcd/kvc"
"go.etcd.io/etcd/client/v3"
v3 "go.etcd.io/etcd/client/v3"
clientv3 "go.etcd.io/etcd/client/v3"
)

const (
Expand Down Expand Up @@ -42,10 +41,10 @@ func (s *Store) CreateNamespace(ctx context.Context, namespace *corev2.Namespace
res, err := s.client.Txn(ctx).
If(
// Ensure the namespace does not already exist
v3.Compare(v3.Version(namespaceKey), "=", 0)).
clientv3.Compare(clientv3.Version(namespaceKey), "=", 0)).
Then(
// Create it
v3.OpPut(namespaceKey, string(namespaceBytes)),
clientv3.OpPut(namespaceKey, string(namespaceBytes)),
).Commit()
if err != nil {
return &store.ErrInternal{Message: err.Error()}
Expand All @@ -68,14 +67,14 @@ func (s *Store) DeleteNamespace(ctx context.Context, name string) error {
err := kvc.Backoff(ctx).Retry(func(n int) (done bool, err error) {
// Validate whether there are any resources referencing the namespace
getresp, err = s.client.Txn(ctx).Then(
v3.OpGet(checkKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(entityConfigKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(assetKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(handlerKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(mutatorKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(eventFilterKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(hookKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(silencedKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
clientv3.OpGet(checkKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
clientv3.OpGet(entityConfigKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
clientv3.OpGet(assetKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
clientv3.OpGet(handlerKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
clientv3.OpGet(mutatorKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
clientv3.OpGet(eventFilterKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
clientv3.OpGet(hookKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
clientv3.OpGet(silencedKeyBuilder.WithNamespace(name).Build(), clientv3.WithPrefix(), clientv3.WithCountOnly()),
).Commit()
return kvc.RetryRequest(n, err)
})
Expand Down
Loading

0 comments on commit 2e20ead

Please sign in to comment.