Adds golangci-lint

This commit is contained in:
Miguel de la Cruz 2023-07-11 16:43:20 +02:00
parent 3335cfe795
commit 1747612b86
9 changed files with 22 additions and 14 deletions

View File

@ -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

View File

@ -11,6 +11,9 @@ test-watch:
fmt:
go fmt ./...
lint:
golangci-lint run ./...
run:
go run ./cmd/birthdaybot -config example-config.yml

View File

@ -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
}

View File

@ -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())

View File

@ -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")

View File

@ -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()
}

View File

@ -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)
}
}

View File

@ -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)
}
}
}

View File

@ -15,6 +15,7 @@ pkgs.mkShell {
gnumake
modd
mockgen
golangci-lint
];
shellHook = ''