From 1747612b86977de369ec9c9467d48ceb2c21725a Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Tue, 11 Jul 2023 16:43:20 +0200 Subject: [PATCH] Adds golangci-lint --- .gitlab-ci.yml | 3 ++- Makefile | 3 +++ model/config.go | 4 ++-- model/config_test.go | 5 ++--- parser/csv_parser_test.go | 4 ++-- server/helpers_test.go | 7 +++---- server/web.go | 5 ++++- server/worker.go | 4 +++- shell.nix | 1 + 9 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 11967c5..dd9788c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,11 +9,12 @@ check-generate: script: - nix-shell --run "make generate && git diff --quiet" -check-fmt: +check-lint: stage: format image: nixos/nix:latest script: - nix-shell --run "make fmt && git diff --quiet" + - nix-shell --run "make lint" check-gomod: stage: format diff --git a/Makefile b/Makefile index 428f401..7060845 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,9 @@ test-watch: fmt: go fmt ./... +lint: + golangci-lint run ./... + run: go run ./cmd/birthdaybot -config example-config.yml diff --git a/model/config.go b/model/config.go index 9ec6a18..c732011 100644 --- a/model/config.go +++ b/model/config.go @@ -3,7 +3,7 @@ package model import ( "errors" "fmt" - "io/ioutil" + "os" "strings" "gopkg.in/yaml.v3" @@ -141,7 +141,7 @@ func (tnc *TelegramNotificationsConfig) IsValid() error { } func ReadConfig(path string) (*Config, error) { - fileBytes, err := ioutil.ReadFile(path) + fileBytes, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/model/config_test.go b/model/config_test.go index b744133..81f88af 100644 --- a/model/config_test.go +++ b/model/config_test.go @@ -2,7 +2,6 @@ package model import ( "io" - "io/ioutil" "os" "testing" @@ -11,7 +10,7 @@ import ( func TestReadConfig(t *testing.T) { t.Run("should correctly read a configuration file", func(t *testing.T) { - f, err := ioutil.TempFile("", "birthdaybot-") + f, err := os.CreateTemp("", "birthdaybot-") require.NoError(t, err) defer os.Remove(f.Name()) @@ -25,7 +24,7 @@ func TestReadConfig(t *testing.T) { }) t.Run("should fail if the file doesn't exist", func(t *testing.T) { - f, err := ioutil.TempFile("", "birthdaybot-") + f, err := os.CreateTemp("", "birthdaybot-") require.NoError(t, err) f.Close() os.Remove(f.Name()) diff --git a/parser/csv_parser_test.go b/parser/csv_parser_test.go index d0b977b..f4ae075 100644 --- a/parser/csv_parser_test.go +++ b/parser/csv_parser_test.go @@ -2,7 +2,7 @@ package parser import ( "io" - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/require" @@ -10,7 +10,7 @@ import ( func TestParseCsv(t *testing.T) { t.Run("should correctly parse a valid CSV file", func(t *testing.T) { - f, err := ioutil.TempFile("", "birthdaybot-") + f, err := os.CreateTemp("", "birthdaybot-") require.NoError(t, err) _, werr := io.WriteString(f, "John Doe , john@doe.com, 1234, 17/04/2192\nJane Doe,jane@doe.com,4321,15/01/2020\n") diff --git a/server/helpers_test.go b/server/helpers_test.go index 9757bc0..d198be6 100644 --- a/server/helpers_test.go +++ b/server/helpers_test.go @@ -2,7 +2,6 @@ package server import ( "fmt" - "io/ioutil" "os" "testing" @@ -26,7 +25,7 @@ type TestHelper struct { } func testConfig(t *testing.T) *model.Config { - f, err := ioutil.TempFile("", "birthdaybot-") + f, err := os.CreateTemp("", "birthdaybot-") require.NoError(t, err) require.NoError(t, f.Close()) require.NoError(t, os.Remove(f.Name())) @@ -72,7 +71,7 @@ func SetupTestHelper(t *testing.T, opts ...Option) *TestHelper { th.srv, err = New(serverOpts...) require.NoError(t, err) - th.srv.Start() + require.NoError(t, th.srv.Start()) client, err := client.New(client.WithURL(fmt.Sprintf("http://127.0.0.1:%d", th.srv.WebServer.Port()))) require.NoError(t, err) @@ -82,6 +81,6 @@ func SetupTestHelper(t *testing.T, opts ...Option) *TestHelper { } func (th *TestHelper) TearDown() { - th.srv.Stop() + require.NoError(th.t, th.srv.Stop()) th.ctrl.Finish() } diff --git a/server/web.go b/server/web.go index d2e1dfd..176ffc6 100644 --- a/server/web.go +++ b/server/web.go @@ -81,5 +81,8 @@ func (ws *WebServer) JSON(w http.ResponseWriter, statusCode int, data any) { } w.WriteHeader(statusCode) - w.Write(b) + if _, err := w.Write(b); err != nil { + ws.logger.Error("cannot write to response writer", "error", err) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + } } diff --git a/server/worker.go b/server/worker.go index 1676454..99b7658 100644 --- a/server/worker.go +++ b/server/worker.go @@ -48,7 +48,9 @@ func (w *SimpleWorker) notifyDay(year, month, day int) { for _, b := range birthdays { w.logger.Info("notifying for birthday", "name", b.Name) - w.server.Notify(b) + if err := w.server.Notify(b); err != nil { + w.logger.Error("error notifying for birthday", "name", b.Name, "error", err) + } } } diff --git a/shell.nix b/shell.nix index dd84f05..c83ebbf 100644 --- a/shell.nix +++ b/shell.nix @@ -15,6 +15,7 @@ pkgs.mkShell { gnumake modd mockgen + golangci-lint ]; shellHook = ''