Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ex0rcist committed Oct 30, 2024
1 parent 190a463 commit 6657b96
Show file tree
Hide file tree
Showing 31 changed files with 1,423 additions and 463 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SERVER_VERSION ?= 0.1.0
BUILD_DATE ?= $(shell date +%F\ %H:%M:%S)
BUILD_COMMIT ?= $(shell git rev-parse --short HEAD)

build: agent server
build: agent server staticlint
.PHONY: build

agent: ## build agent
Expand All @@ -22,7 +22,7 @@ agent: ## build agent

server: ## build server
rm -rf $(API_DOCS)
swag init -g ./internal/httpserver/handlers.go --output $(API_DOCS)
swag init -g ./internal/httpserver/router.go --output docs/api

go build \
-ldflags "\
Expand All @@ -34,6 +34,10 @@ server: ## build server
cmd/$@/*.go
.PHONY: server

staticlint: ## build static lint
go build -o cmd/$@/$@ cmd/$@/*.go
.PHONY: staticlint

clean: ## remove build artifacts
rm -rf cmd/agent/agent cmd/server/server cmd/staticlint/staticlint
.PHONY: clean
Expand Down
8 changes: 8 additions & 0 deletions cmd/staticlint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "github.com/ex0rcist/metflix/pkg/staticlint"

func main() {
lint := staticlint.New()
lint.Run()
}
Binary file added cmd/staticlint/staticlint
Binary file not shown.
718 changes: 517 additions & 201 deletions coverage.html

Large diffs are not rendered by default.

514 changes: 288 additions & 226 deletions coverage.out.tmp

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
github.com/go-openapi/swag v0.23.0
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/jackc/pgx/v5 v5.6.0
github.com/jingyugao/rowserrcheck v1.1.1
github.com/kisielk/errcheck v1.8.0
github.com/klauspost/compress v1.15.11
github.com/rs/zerolog v1.33.0
github.com/satori/go.uuid v1.2.0
Expand All @@ -20,10 +22,14 @@ require (
github.com/stretchr/testify v1.9.0
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.4
github.com/timakin/bodyclose v0.0.0-20241017074824-adbc21e6bf36
golang.org/x/sync v0.8.0
golang.org/x/tools v0.26.0
honnef.co/go/tools v0.5.1
)

require (
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -33,6 +39,8 @@ require (
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand All @@ -56,11 +64,12 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20231108232855-2478ac86f678 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
79 changes: 77 additions & 2 deletions go.sum

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions internal/agent/exporter/batch_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ func (e *BatchExporter) doSend() error {
req.Header.Set("X-Request-Id", requestID)

if e.signer != nil {
signature, err := e.signer.CalculateSignature(payload.Bytes())
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", err.Error())
return err
signature, signErr := e.signer.CalculateSignature(payload.Bytes())
if signErr != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", signErr.Error())
return signErr
}

req.Header.Set("HashSHA256", signature)
Expand All @@ -152,7 +152,12 @@ func (e *BatchExporter) doSend() error {
return entities.RetriableError{Err: err, RetryAfter: 10 * time.Second}
}

defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error reading response body", err.Error())
Expand Down
15 changes: 10 additions & 5 deletions internal/agent/exporter/limited_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ func (e *LimitedExporter) doSend(mex metrics.MetricExchange) error {
req.Header.Set("X-Request-Id", requestID)

if e.signer != nil {
signature, err := e.signer.CalculateSignature(payload.Bytes())
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", err.Error())
return err
signature, signErr := e.signer.CalculateSignature(payload.Bytes())
if signErr != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error during signing", signErr.Error())
return signErr
}

req.Header.Set("HashSHA256", signature)
Expand All @@ -185,7 +185,12 @@ func (e *LimitedExporter) doSend(mex metrics.MetricExchange) error {
return entities.RetriableError{Err: err, RetryAfter: 10 * time.Second}
}

defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
logging.LogErrorCtx(ctx, entities.ErrMetricReport, "error reading response body", err.Error())
Expand Down
8 changes: 7 additions & 1 deletion internal/compression/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"testing"

"github.com/ex0rcist/metflix/internal/logging"
"github.com/klauspost/compress/gzip"
)

Expand All @@ -19,7 +20,12 @@ func TestPack_Success(t *testing.T) {
if err != nil {
t.Fatalf("expected no error creating gzip reader, got %v", err)
}
defer reader.Close()

defer func() {
if closeErr := reader.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

unpackedData, err := io.ReadAll(reader)
if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions internal/compression/compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"testing"

"github.com/ex0rcist/metflix/internal/logging"
"github.com/klauspost/compress/gzip"
)

Expand All @@ -28,7 +29,12 @@ func TestCompressor_Write_SupportedContent(t *testing.T) {
compressor.Close()

resp := recorder.Result()
defer resp.Body.Close()

defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

if resp.Header.Get("Content-Encoding") != "gzip" {
t.Fatalf("expected Content-Encoding to be gzip, got %s", resp.Header.Get("Content-Encoding"))
Expand All @@ -38,7 +44,12 @@ func TestCompressor_Write_SupportedContent(t *testing.T) {
if err != nil {
t.Fatalf("expected no error creating gzip reader, got %v", err)
}
defer gr.Close()

defer func() {
if closeErr := gr.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

uncompressedData := new(bytes.Buffer)
_, err = uncompressedData.ReadFrom(gr)
Expand Down Expand Up @@ -66,7 +77,11 @@ func TestCompressor_Write_UnsupportedContent(t *testing.T) {
}

resp := recorder.Result()
defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

if resp.Header.Get("Content-Encoding") == "gzip" {
t.Fatalf("expected Content-Encoding to not be gzip")
Expand Down
12 changes: 10 additions & 2 deletions internal/compression/decompressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/klauspost/compress/gzip"

"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"

"net/http"
"net/http/httptest"
Expand All @@ -24,7 +25,10 @@ func TestDecompressor_Decompress_SupportedEncoding(t *testing.T) {
t.Fatalf("expected no error on writer.Write(), got %v", err)
}

writer.Close()
err = writer.Close()
if err != nil {
logging.LogError(err)
}

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Encoding", "gzip")
Expand Down Expand Up @@ -95,7 +99,11 @@ func TestDecompressor_Close(t *testing.T) {
if err != nil {
t.Fatalf("expected no error on writer.Write, got %v", err)
}
writer.Close()

err = writer.Close()
if err != nil {
logging.LogError(err)
}

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Encoding", "gzip")
Expand Down
2 changes: 1 addition & 1 deletion internal/httpserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (r MetricResource) UpdateMetricJSON(rw http.ResponseWriter, req *http.Reque
// @Tags Metrics
// @Router /updates [post]
// @Summary Push list of metrics data as JSON
// @ID update_metrics_batch
// @ID metrics_json_update_list
// @Accept json
// @Param request body []metrics.MetricExchange true "List of metrics to update."
// @Success 200 {object} []metrics.MetricExchange
Expand Down
7 changes: 6 additions & 1 deletion internal/httpserver/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/ex0rcist/metflix/internal/entities"
"github.com/ex0rcist/metflix/internal/logging"
"github.com/ex0rcist/metflix/internal/services"
"github.com/ex0rcist/metflix/internal/storage"
"github.com/ex0rcist/metflix/pkg/metrics"
Expand All @@ -29,7 +30,11 @@ func testRequest(t *testing.T, router http.Handler, method, path string, payload
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)

defer resp.Body.Close()
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

contentType := resp.Header.Get("Content-Type")

Expand Down
27 changes: 23 additions & 4 deletions internal/middleware/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"testing"

"github.com/ex0rcist/metflix/internal/logging"
"github.com/klauspost/compress/gzip"
)

Expand Down Expand Up @@ -43,7 +44,10 @@ func TestDecompressRequest_Success(t *testing.T) {
t.Fatalf("expected no error writing writer.Write(), got %v", err)
}

writer.Close()
err = writer.Close()
if err != nil {
logging.LogError(err)
}

req := httptest.NewRequest(http.MethodPost, "/", &buf)
req.Header.Set("Content-Encoding", "gzip")
Expand Down Expand Up @@ -137,7 +141,12 @@ func TestCompressResponse_Success(t *testing.T) {
handler.ServeHTTP(rr, req)

resp := rr.Result()
defer resp.Body.Close()
defer func() {
err := resp.Body.Close()
if err != nil {
logging.LogError(err)
}
}()

if resp.Header.Get("Content-Encoding") != "gzip" {
t.Fatalf("expected Content-Encoding to be gzip, got %s", resp.Header.Get("Content-Encoding"))
Expand All @@ -147,7 +156,12 @@ func TestCompressResponse_Success(t *testing.T) {
if err != nil {
t.Fatalf("expected no error creating gzip reader, got %v", err)
}
defer gr.Close()

defer func() {
if closeErr := gr.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

decompressedData, err := io.ReadAll(gr)
if err != nil {
Expand Down Expand Up @@ -177,7 +191,12 @@ func TestCompressResponse_NoCompressionRequested(t *testing.T) {
handler.ServeHTTP(rr, req)

resp := rr.Result()
defer resp.Body.Close()
defer func() {
err := resp.Body.Close()
if err != nil {
logging.LogError(err)
}
}()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/middleware/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ func CheckSignedRequest(next http.Handler, secret entities.Secret) http.Handler
}

bodyBytes, err := io.ReadAll(r.Body)
r.Body.Close() // must close
defer func() {
if closeErr := r.Body.Close(); closeErr != nil {
logging.LogError(closeErr)
}
}()

if err != nil {
logging.LogErrorCtx(ctx, fmt.Errorf("failed to read request body"))
http.Error(w, "failed to read request body", http.StatusInternalServerError)
Expand Down
Loading

0 comments on commit 6657b96

Please sign in to comment.