Skip to content

Commit

Permalink
added lint job to the ci
Browse files Browse the repository at this point in the history
  • Loading branch information
kopaygorodsky committed May 29, 2021
1 parent a052238 commit b3f6000
Show file tree
Hide file tree
Showing 45 changed files with 279 additions and 238 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
sleep 1
done
echo Failed waiting for MySQL && exit 1
- run:
name: Lint
command: |
make lint
- run:
name: Run tests
environment:
Expand Down
8 changes: 7 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ linters:
- varcheck
- goimports
- typecheck
- errorlint
#- errorlint
- gofmt
- misspell
- nestif
Expand All @@ -57,6 +57,12 @@ linters-settings:
go: "1.16"
unused:
go: "1.16"
gosec:
# Available rules: https://github.com/securego/gosec#available-rules
excludes:
- G404
nestif:
min-complexity: 8


issues:
Expand Down
18 changes: 11 additions & 7 deletions log/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ type defaultLogger struct {
internalLogger *log.Logger
}

func (l defaultLogger) Log(level Level, v... interface{}) {
func (l defaultLogger) Log(level Level, v ...interface{}) {
if level == FatalLevel {
l.internalLogger.Fatal(v...)
return
}
l.internalLogger.Output(3, fmt.Sprint(v...))
}

func (l defaultLogger) Logf(level Level, template string, args... interface{}) {
if level == FatalLevel {
l.internalLogger.Fatalf(template, args...)
if level == PanicLevel {
l.internalLogger.Panic(v...)
return
}
l.internalLogger.Output(3, fmt.Sprintf(template, args...))

if err := l.internalLogger.Output(3, fmt.Sprint(v...)); err != nil {
l.internalLogger.Println(fmt.Sprintf("err logging an entry: %s. %s", err, v))
}
}

func (l defaultLogger) Logf(level Level, template string, args ...interface{}) {
l.Log(level, fmt.Sprintf(template, args...))
}

func (l defaultLogger) SetLevel(level Level) {
Expand Down
1 change: 1 addition & 0 deletions messagebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package brigadier

import (
"errors"

"github.com/go-foreman/foreman/log"
"github.com/go-foreman/foreman/pubsub/dispatcher"
"github.com/go-foreman/foreman/pubsub/endpoint"
Expand Down
9 changes: 5 additions & 4 deletions pubsub/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package dispatcher

import (
"fmt"
"reflect"

"github.com/go-foreman/foreman/pubsub/message"
"github.com/go-foreman/foreman/pubsub/message/execution"
"github.com/go-foreman/foreman/runtime/scheme"
"reflect"
)

type Dispatcher interface {
Expand All @@ -17,14 +18,14 @@ type Dispatcher interface {

func NewDispatcher() Dispatcher {
return &dispatcher{
handlers: make(map[reflect.Type][]execution.Executor),
handlers: make(map[reflect.Type][]execution.Executor),
listeners: make(map[reflect.Type][]execution.Executor),
}
}

type dispatcher struct {
handlers map[reflect.Type][]execution.Executor
listeners map[reflect.Type][]execution.Executor
handlers map[reflect.Type][]execution.Executor
listeners map[reflect.Type][]execution.Executor
allEvsListeners []execution.Executor
}

Expand Down
14 changes: 5 additions & 9 deletions pubsub/dispatcher/dispatcher_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package dispatcher

import (
"reflect"
"testing"

"github.com/go-foreman/foreman/pubsub/message"
"github.com/go-foreman/foreman/pubsub/message/execution"
"github.com/go-foreman/foreman/runtime/scheme"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"reflect"
"testing"
)

type registerAccountCmd struct {
Expand All @@ -26,10 +27,6 @@ type confirmationSentEvent struct {
message.ObjectMeta
}

func allExecutor(execCtx execution.MessageExecutionCtx) error {
return nil
}

type service struct {
}

Expand Down Expand Up @@ -77,7 +74,7 @@ func TestDispatcher_SubscribeForCmd(t *testing.T) {
dispatcher.SubscribeForCmd(wrongType, handler.handle)
})
})

t.Run("cross subscription for an event", func(t *testing.T) {
dispatcher := NewDispatcher()
dispatcher.SubscribeForCmd(&registerAccountCmd{}, handler.handle)
Expand Down Expand Up @@ -140,7 +137,7 @@ func TestDispatcher_SubscribeForAllEvents(t *testing.T) {
require.Len(t, listeners, 1)
assertThisValueExists(t, handler.handle, listeners)
})

t.Run("subscribe for an event by duplicating handler for all events", func(t *testing.T) {
dispatcher := NewDispatcher()
dispatcher.SubscribeForAllEvents(handler.handle)
Expand Down Expand Up @@ -182,4 +179,3 @@ func assertThisValueExists(t *testing.T, expected execution.Executor, executors
}
assert.True(t, exists, "expected executor is not found among executors")
}

11 changes: 5 additions & 6 deletions pubsub/endpoint/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package endpoint

import (
"context"
"time"

"github.com/go-foreman/foreman/pubsub/message"
"github.com/go-foreman/foreman/pubsub/transport"
"github.com/go-foreman/foreman/pubsub/transport/pkg"
"github.com/pkg/errors"
"time"
)

type AmqpEndpoint struct {
Expand All @@ -27,11 +28,9 @@ func (a AmqpEndpoint) Name() string {
func (a AmqpEndpoint) Send(ctx context.Context, msg *message.OutcomingMessage, opts ...DeliveryOption) error {
deliveryOpts := &deliveryOptions{}

if opts != nil {
for _, opt := range opts {
if err := opt(deliveryOpts); err != nil {
return errors.Wrapf(err, "error compiling delivery options for message %s", msg.UID())
}
for _, opt := range opts {
if err := opt(deliveryOpts); err != nil {
return errors.Wrapf(err, "error compiling delivery options for message %s", msg.UID())
}
}

Expand Down
4 changes: 2 additions & 2 deletions pubsub/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package endpoint

import (
"context"
"github.com/go-foreman/foreman/pubsub/message"
"time"

"github.com/go-foreman/foreman/pubsub/message"
)

type Endpoint interface {
Expand All @@ -13,7 +14,6 @@ type Endpoint interface {

type deliveryOptions struct {
delay *time.Duration
headers message.Headers
}

func WithDelay(delay time.Duration) DeliveryOption {
Expand Down
7 changes: 4 additions & 3 deletions pubsub/endpoint/router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package endpoint

import (
"reflect"

"github.com/go-foreman/foreman/pubsub/message"
"github.com/go-foreman/foreman/runtime/scheme"
"reflect"
)

type Router interface {
RegisterEndpoint(endpoint Endpoint, objects... message.Object)
RegisterEndpoint(endpoint Endpoint, objects ...message.Object)
Route(obj message.Object) []Endpoint
}

Expand All @@ -21,7 +22,7 @@ type router struct {
routes map[reflect.Type][]Endpoint
}

func (r *router) RegisterEndpoint(endpoint Endpoint, objects... message.Object) {
func (r *router) RegisterEndpoint(endpoint Endpoint, objects ...message.Object) {
for _, obj := range objects {
structType := scheme.GetStructType(obj)
r.routes[structType] = append(r.routes[structType], endpoint)
Expand Down
1 change: 1 addition & 0 deletions pubsub/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package errors

//
//type Status int
//
Expand Down
10 changes: 5 additions & 5 deletions pubsub/message/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package message

import (
"encoding/json"
"reflect"
"strings"

"github.com/go-foreman/foreman/runtime/scheme"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"reflect"
"strings"
)

type Marshaller interface {
Expand All @@ -28,7 +29,6 @@ func WithDecoderErr(err error) error {

type jsonDecoder struct {
knownTypes scheme.KnownTypesRegistry
decoder mapstructure.Decoder
}

func (j jsonDecoder) Unmarshal(b []byte) (Object, error) {
Expand Down Expand Up @@ -162,7 +162,7 @@ func (j jsonDecoder) Marshal(obj Object) ([]byte, error) {

var objectType = reflect.TypeOf((*Object)(nil)).Elem()

func(j jsonDecoder) setGroupKind(obj Object) error {
func (j jsonDecoder) setGroupKind(obj Object) error {
if gk := obj.GroupKind(); gk.Empty() {
gk, err := j.knownTypes.ObjectKind(obj)
if err != nil {
Expand All @@ -188,4 +188,4 @@ func(j jsonDecoder) setGroupKind(obj Object) error {
}

return nil
}
}
22 changes: 10 additions & 12 deletions pubsub/message/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package message

import (
"encoding/json"
"strings"
"testing"

"github.com/go-foreman/foreman/runtime/scheme"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strings"
"testing"
)

const (
Expand All @@ -16,13 +17,13 @@ const (
type WrapperType struct {
ObjectMeta
Nested Object
Value int
Value int
}

type SomeTypeWithNestedType struct {
ObjectMeta
Nested Object
Value int
Value int
}

type SomeTestType struct {
Expand All @@ -35,8 +36,6 @@ type ChildType struct {
Value int
}



func TestJsonDecoder(t *testing.T) {
knownRegistry := scheme.NewKnownTypesRegistry()
decoder := NewJsonMarshaller(knownRegistry)
Expand All @@ -50,7 +49,7 @@ func TestJsonDecoder(t *testing.T) {
Group: group.String(),
},
},
Value: 1,
Value: 1,
}

marshaled, err := decoder.Marshal(instance)
Expand All @@ -66,7 +65,7 @@ func TestJsonDecoder(t *testing.T) {
t.Run("verify that GK is set from schema before encoding", func(t *testing.T) {
knownRegistry.AddKnownTypes(group, &SomeTestType{})
instance := &SomeTestType{
Value: 1,
Value: 1,
}
marshaled, err := decoder.Marshal(instance)
require.NoError(t, err)
Expand All @@ -77,7 +76,7 @@ func TestJsonDecoder(t *testing.T) {
assert.IsType(t, &SomeTestType{}, decodedObj)
assert.Equal(t, instance.Value, instance.Value)
})

t.Run("decode invalid payload with empty GK", func(t *testing.T) {
instance := &SomeTestType{
ObjectMeta: ObjectMeta{
Expand All @@ -86,7 +85,7 @@ func TestJsonDecoder(t *testing.T) {
Group: group.String(),
},
},
Value: 1,
Value: 1,
}

marshaled, err := json.Marshal(instance)
Expand All @@ -111,7 +110,7 @@ func TestJsonDecoder(t *testing.T) {
knownRegistry.AddKnownTypes(group, &WrapperType{})
instance := &WrapperType{
Nested: &SomeTypeWithNestedType{
Nested: &SomeTestType{
Nested: &SomeTestType{
Value: 1,
Child: ChildType{
Value: -1,
Expand All @@ -131,4 +130,3 @@ func TestJsonDecoder(t *testing.T) {
assert.EqualValues(t, instance, decodedObj)
})
}

1 change: 1 addition & 0 deletions pubsub/message/execution/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package execution

import (
"context"

"github.com/go-foreman/foreman/log"
"github.com/go-foreman/foreman/pubsub/endpoint"
"github.com/go-foreman/foreman/pubsub/message"
Expand Down
Loading

0 comments on commit b3f6000

Please sign in to comment.