Skip to content
This repository has been archived by the owner on Dec 30, 2018. It is now read-only.

Commit

Permalink
ci: add more linters
Browse files Browse the repository at this point in the history
Add more linters and fix all the new failing cases.

Closes: #119
  • Loading branch information
diegobernardes committed Feb 18, 2018
1 parent 69aa28b commit 5de13f2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ lint-slow:
--enable=testify \
--enable=unconvert \
--enable=varcheck \
--enable=nakedret \
--enable=unparam \
--deadline=20m \
--aggregate \
--line-length=100 \
Expand Down
4 changes: 2 additions & 2 deletions domain/subscription/http/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *subscriptionCreate) valid(resource *flare.Resource) error {
return errors.Wrap(err, "invalid endpoint")
}

if err := s.validEndpointMethod(resource); err != nil {
if err := s.validEndpointMethod(); err != nil {
return errors.Wrap(err, "invalid method")
}

Expand Down Expand Up @@ -295,7 +295,7 @@ outer:
return nil
}

func (s *subscriptionCreate) validEndpointMethod(resource *flare.Resource) error {
func (s *subscriptionCreate) validEndpointMethod() error {
valid := func(method string) bool {
switch method {
case "", http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
Expand Down
8 changes: 3 additions & 5 deletions domain/subscription/worker/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ func (d *Delivery) buildRequest(
}
}

addr, err := d.buildEndpoint(resource, subscription, endpoint, rawAddr)
addr, err := d.buildEndpoint(resource, endpoint, rawAddr)
if err != nil {
return nil, errors.Wrap(err, "error during endpoint generate")
}

req, err := d.buildRequestHTTP(ctx, content, subscription, addr, method, headers)
req, err := d.buildRequestHTTP(ctx, content, addr, method, headers)
if err != nil {
return nil, err
}
Expand All @@ -256,7 +256,6 @@ func (d *Delivery) buildRequest(
func (d *Delivery) buildRequestHTTP(
ctx context.Context,
content []byte,
subscription *flare.Subscription,
addr, method string,
headers http.Header,
) (*http.Request, error) {
Expand All @@ -267,7 +266,7 @@ func (d *Delivery) buildRequestHTTP(
}
req = req.WithContext(ctx)

req.Header = subscription.Endpoint.Headers
req.Header = headers
contentType := req.Header.Get("content-type")
if contentType == "" && len(content) > 0 {
req.Header.Add("Content-Type", "application/json")
Expand All @@ -278,7 +277,6 @@ func (d *Delivery) buildRequestHTTP(

func (d *Delivery) buildEndpoint(
resource *flare.Resource,
subscription *flare.Subscription,
endpoint *url.URL,
rawSubscriptionEndpoint fmt.Stringer,
) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions infra/wildcard/wildcard.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ func validContentStruct(content string, offset, indexStart, indexEnd int) (strin
}

value := strings.TrimSpace(content[offset+indexStart+1 : offset+indexEnd])
if err := validFragment(content, value, offset, indexStart, indexEnd); err != nil {
if err := validFragment(content, value, offset, indexStart); err != nil {
return "", err
}

return value, nil
}

func validFragment(content, value string, offset, indexStart, indexEnd int) error {
func validFragment(content, value string, offset, index int) error {
if strings.Contains(value, "{") {
return errors.New("found a '{' inside a wildcard")
}
Expand All @@ -271,7 +271,7 @@ func validFragment(content, value string, offset, indexStart, indexEnd int) erro
return errors.New("missing the wildcard id")
}

if offset > 0 && (content[offset+indexStart-1] == '}' && content[offset+indexStart] == '{') {
if offset > 0 && (content[offset+index] == '{' && content[offset+index-1] == '}') {
return errors.New("there must be at least one char between wildcard")
}
return nil
Expand Down
28 changes: 16 additions & 12 deletions provider/aws/queue/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ func (s *SQS) Pull(ctx context.Context, fn func(context.Context, []byte) error)
return nil
}

func (s *SQS) sqsEndpoint() (string, error) {
result, err := s.client.GetQueueUrl(&sqs.GetQueueUrlInput{QueueName: aws.String(s.name)})
func (s *SQS) sqsEndpoint(ctx context.Context) (string, error) {
result, err := s.client.GetQueueUrlWithContext(
ctx, &sqs.GetQueueUrlInput{QueueName: aws.String(s.name)},
)
if err != nil {
awsErr, ok := err.(interface {
Code() string
Expand All @@ -94,16 +96,18 @@ func (s *SQS) sqsEndpoint() (string, error) {
return *result.QueueUrl, nil
}

func (s *SQS) createSQS() (string, error) {
output, err := s.client.CreateQueue(&sqs.CreateQueueInput{QueueName: aws.String(s.name)})
func (s *SQS) createSQS(ctx context.Context) error {
_, err := s.client.CreateQueueWithContext(
ctx, &sqs.CreateQueueInput{QueueName: aws.String(s.name)},
)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("error during queue '%s' create", s.name))
return errors.Wrap(err, fmt.Sprintf("error during queue '%s' create", s.name))
}

for {
queue, err := s.sqsEndpoint()
queue, err := s.sqsEndpoint(ctx)
if err != nil {
return "", errors.Wrap(err, "error during waiting for SQS queue to be created")
return errors.Wrap(err, "error during waiting for SQS queue to be created")
}

if queue != "" {
Expand All @@ -113,25 +117,25 @@ func (s *SQS) createSQS() (string, error) {
<-time.After(1 * time.Second)
}

return output.String(), nil
return nil
}

// SQSSetup is used to create the queues if not exists.
func SQSSetup(options ...func(*SQS)) error {
func SQSSetup(ctx context.Context, options ...func(*SQS)) error {
s, err := initSQS(options...)
if err != nil {
return err
}

endpoint, err := s.sqsEndpoint()
endpoint, err := s.sqsEndpoint(ctx)
if err != nil {
return errors.Wrap(err, "error during queue find")
}
if endpoint != "" {
return nil
}

if _, err := s.createSQS(); err != nil {
if err := s.createSQS(ctx); err != nil {
return errors.Wrap(err, "error during queue create")
}
return nil
Expand All @@ -144,7 +148,7 @@ func NewSQS(options ...func(*SQS)) (*SQS, error) {
return nil, err
}

endpoint, err := s.sqsEndpoint()
endpoint, err := s.sqsEndpoint(context.Background())
if err != nil {
return nil, errors.Wrap(err, "error during queue find")
}
Expand Down
7 changes: 2 additions & 5 deletions provider/memory/repository/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (s *Subscription) Create(ctx context.Context, subscription *flare.Subscript
}

// HasSubscription check if a resource has subscriptions.
func (s *Subscription) HasSubscription(ctx context.Context, resourceId string) (bool, error) {
func (s *Subscription) HasSubscription(_ context.Context, resourceId string) (bool, error) {
s.mutex.Lock()
defer s.mutex.Unlock()

Expand Down Expand Up @@ -223,9 +223,7 @@ func (s *Subscription) Trigger(
s.mutex.Lock()
defer s.mutex.Unlock()

subscription, doc, err := s.triggerDocumentAndSubscription(
ctx, action, rawDocument, rawSubscription,
)
subscription, doc, err := s.triggerDocumentAndSubscription(ctx, rawDocument, rawSubscription)
if err != nil {
return err
}
Expand Down Expand Up @@ -266,7 +264,6 @@ func (s *Subscription) Trigger(

func (s *Subscription) triggerDocumentAndSubscription(
ctx context.Context,
action string,
rawDocument *flare.Document,
rawSubscription *flare.Subscription,
) (*flare.Subscription, *flare.Document, error) {
Expand Down
2 changes: 1 addition & 1 deletion provider/mongodb/repository/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (d *Document) Delete(_ context.Context, id string) error {
}

func (d *Document) findByIDAndRevision(
ctx context.Context, id string, revision *int64,
_ context.Context, id string, revision *int64,
) (*flare.Document, error) {
session := d.client.Session()
defer session.Close()
Expand Down
3 changes: 1 addition & 2 deletions provider/mongodb/repository/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ func (s *Subscription) newEntry(

func (s *Subscription) triggerProcessDelete(
groupCtx context.Context,
kind string,
session *mgo.Session,
subs *flare.Subscription,
doc *flare.Document,
Expand Down Expand Up @@ -407,7 +406,7 @@ func (s *Subscription) triggerProcess(
referenceDocument.Resource = subs.Resource

if kind == flare.SubscriptionTriggerDelete {
return s.triggerProcessDelete(groupCtx, kind, session, subs, doc, fn)
return s.triggerProcessDelete(groupCtx, session, subs, doc, fn)
}

if newer := doc.Newer(referenceDocument); !newer {
Expand Down
6 changes: 3 additions & 3 deletions provider/test/repository/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func ensureDockerImage(client *docker.Client) error {

var hasImage bool
for _, img := range imgs {
if hasDockerImage(img.RepoTags, dockerImage, dockerTag) {
if hasDockerImage(img.RepoTags) {
hasImage = true
break
}
Expand All @@ -101,8 +101,8 @@ func ensureDockerImage(client *docker.Client) error {
return nil
}

func hasDockerImage(images []string, name, tag string) bool {
key := fmt.Sprintf("%s:%s", name, tag)
func hasDockerImage(images []string) bool {
key := fmt.Sprintf("%s:%s", dockerImage, dockerTag)
for _, image := range images {
if image == key {
return true
Expand Down
5 changes: 3 additions & 2 deletions service/flare/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func (q *queue) setup(ctx context.Context) error {
switch q.cfg.GetString("provider.queue") {
case providerMemory:
case providerAWSSQS:
return q.setupAWSSQS()
return q.setupAWSSQS(ctx)
}

return nil
}

func (q *queue) setupAWSSQS() error {
func (q *queue) setupAWSSQS(ctx context.Context) error {
names := []string{"subscription.partition", "subscription.spread", "subscription.delivery"}

logger := level.Info(q.logger)
Expand All @@ -88,6 +88,7 @@ func (q *queue) setupAWSSQS() error {
logger.Log("message", fmt.Sprintf("creating SQS queue for worker '%s' with name '%s'", name, qn))

err := sqsQueue.SQSSetup(
ctx,
sqsQueue.SQSQueueName(qn),
sqsQueue.SQSSession(q.awsSession),
)
Expand Down

0 comments on commit 5de13f2

Please sign in to comment.