fix: adjust some code structure to satisfy linter
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful

This commit is contained in:
Tom Neuber 2025-04-18 15:31:05 +02:00
parent 7b30a04933
commit 4060ec09c4
Signed by: tom
GPG key ID: F17EFE4272D89FF6
7 changed files with 38 additions and 31 deletions

View file

@ -378,7 +378,7 @@ linters:
# - "default": report only the default slog logger
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#no-global
# Default: ""
no-global: all
no-global: ""
# Enforce using methods that accept a context.
# Values:
# - "": disabled

View file

@ -1,7 +1,6 @@
package apiv1
import (
"log"
"net/http"
"github.com/go-chi/render"
@ -34,6 +33,6 @@ func errRender(err error) render.Renderer {
func renderResponse(w http.ResponseWriter, r *http.Request, v render.Renderer) {
if err := render.Render(w, r, v); err != nil {
log.Fatal(err)
panic(err)
}
}

View file

@ -40,7 +40,10 @@ func (c *Context) Download() error {
}
return err
}
defer resp.Close()
if err = resp.Close(); err != nil {
return err
}
if resp.StatusCode == http.StatusNotFound {
return ErrInvalidURL
}
@ -50,19 +53,19 @@ func (c *Context) Download() error {
if resp.StatusCode != http.StatusOK {
return errors.New(resp.RawResponse.Status)
}
resp.Close()
resp, err = req.Get(c.Link, nil)
if err != nil {
return err
}
defer resp.Close()
var filesize int64
if resp.RawResponse.ContentLength > -1 {
filesize = resp.RawResponse.ContentLength
}
destFile, err := os.OpenFile(c.Filename, os.O_CREATE|os.O_WRONLY, 0644)
destFile, err := os.OpenFile(c.Filename, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}

View file

@ -58,7 +58,10 @@ func TestDownload(t *testing.T) {
t.Run("CertificateError", func(t *testing.T) {
// This test assumes a self-signed certificate or similar issue. This is hard to simulate in a unit test.
ctx := downloader.NewContext("test_cert_error.txt", "https://self-signed.badssl.com/") // Example URL that can be used
ctx := downloader.NewContext(
"test_cert_error.txt",
"https://self-signed.badssl.com/", // Example URL that can be used
)
if err := ctx.Download(); err == nil || err.Error() != "certificate from unknown authority" {
t.Errorf("expected certificate from unknown authority error, got %v", err)

View file

@ -1,7 +1,7 @@
package exporter
import (
"log"
"log/slog"
"github.com/prometheus/client_golang/prometheus"
)
@ -95,7 +95,7 @@ func (m *Metrics) collectCurrentlyCachedMetric() {
func (m *Metrics) collectDatabaseTimestampMetric() {
timestamp, err := m.exporter.database.Timestamp()
if err != nil {
log.Printf("failed to read file timestamp: %v", err)
slog.Warn("failed to read file timestamp", "error", err)
return
}

30
main.go
View file

@ -1,7 +1,7 @@
package main
import (
"log"
"log/slog"
"net/http"
"os"
"os/signal"
@ -24,7 +24,7 @@ func main() {
cli := cmd.CLI{}
config, err := cli.Parse()
if err != nil {
log.Fatal(err)
panic(err)
}
handleGracefulShutdown()
@ -38,15 +38,16 @@ func main() {
ctx := downloader.NewContext(config.DataFile, config.DataURL)
if !ctx.FileExists() {
if downloadErr := ctx.Download(); downloadErr != nil {
log.Fatal(downloadErr)
panic(err)
}
log.Printf("saved file to %s\n", ctx.Filename)
slog.Info("saved file", "path", ctx.Filename)
}
cache := cache.NewCache(config.CacheTTL)
db, err := database.NewDatabase(config)
if err != nil {
log.Fatal("database creation failed", err)
slog.Error("database creation failed")
panic(err)
}
if config.EnableExporter {
@ -66,23 +67,24 @@ func main() {
}
}
}()
log.Println("prometheus exporter refreshes metric data every", config.ExporterInterval)
slog.Info("prometheus exporter refreshes metric data", "interval", config.ExporterInterval)
go func() {
err = exporter.Start()
if err != nil {
log.Panic(err)
panic(err)
}
}()
log.Println("prometheus exporter started at", config.ExporterAddress)
slog.Info("prometheus exporter started", "address", config.ExporterAddress)
}
log.Println("importing data from file", config.DataFile)
slog.Info("importing data from file", "path", config.DataFile)
err = csvimporter.ImportCSV(config.DataFile, db)
if err != nil {
log.Fatal("data Import from file failed", err)
slog.Error("data import from file failed")
panic(err)
}
log.Println("imported data from file successful")
slog.Info("imported data from file successfully")
lh := apiv1.NewLocationHandler(cache, db)
r.Mount("/api/v1", apiv1.NewRouter(lh))
@ -93,9 +95,9 @@ func main() {
ReadHeaderTimeout: config.ReadHeaderTimeout,
}
log.Println("starting server at", server.Addr)
slog.Info("starting server", "address", server.Addr)
if err = server.ListenAndServe(); err != nil {
log.Panic(err)
panic(err)
}
}
@ -106,7 +108,7 @@ func handleGracefulShutdown() {
go func() {
sig := <-signals
log.Printf("caught signal: %+v", sig)
slog.Info("caught signal", "signal", sig)
os.Exit(0)
}()
}