Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch logging to slog #111

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 79 additions & 6 deletions .github/linters/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,113 @@
# configure golangci-lint
# based on https://github.com/github/super-linter/blob/main/TEMPLATES/.golangci.yml
linters:
# see https://golangci-lint.run/usage/linters/
enable:
- gosec
- unconvert
- gocyclo
- goconst
- goimports
- gocritic
- govet
- revive
- gofmt
- gci
- gosimple
- ineffassign
- gocheckcompilerdirectives
- sloglint
- unconvert
- whitespace
linters-settings:
errcheck:
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: true
gocyclo:
# minimal code complexity to report, 30 by default
min-complexity: 15
# List of functions to exclude from checking, where each entry is a single function to exclude.
# See https://github.com/kisielk/errcheck#excluding-functions for details.
exclude-functions:
- io.Copy
- fmt.Printf
- bytes.Buffer.Write
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
revive:
# disable all rules by default in case new linters will be added
# enable this flag from time to time to see if we should fix/add more here
enable-all-rules: false
# see https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
rules:
- name: exported
disabled: false
arguments:
- disableStutteringCheck
- name: import-alias-naming
disabled: false
arguments: ['^[a-z][a-zA-Z0-9]{0,}$']
- name: context-as-argument
disabled: false
- name: import-shadowing
disabled: false
- name: var-declaration
disabled: false
- name: use-any
disabled: false
- name: cognitive-complexity
disabled: false
arguments: [30]
- name: cyclomatic
disabled: false
arguments: [30]
- name: line-length-limit
disabled: false
arguments: [180]
- name: comment-spacings
disabled: false
- name: increment-decrement
disabled: false
- name: unhandled-error
disabled: false
arguments:
- 'fmt.Printf'
- 'io.Copy'
- 'bytes.Buffer.Write'
- name: var-naming
disabled: false

- name: add-constant
disabled: true
- name: bare-return
disabled: true
- name: flag-parameter
disabled: true
- name: function-length
disabled: true
- name: unused-receiver
disabled: true
- name: max-public-structs
disabled: true
- name: unchecked-type-assertion
disabled: true
- name: function-result-limit
disabled: true
- name: confusing-naming
disabled: true
- name: get-return
disabled: true
- name: nested-structs
disabled: true
- name: argument-limit
disabled: true
gci:
custom-order: true
sections:
- standard
- default
- prefix(github.com/vgarvardt/rklotz)
staticcheck:
checks:
- all

issues:
exclude-rules:
Expand All @@ -45,7 +119,6 @@ issues:
- goconst
- staticcheck
include:
# based on the
# https://www.pixelstech.net/article/1650069870-golangci-lint-to-enable-comment-check-for-exported-functions
# based on the https://www.pixelstech.net/article/1650069870-golangci-lint-to-enable-comment-check-for-exported-functions
- EXC0012 # EXC0012 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0014 # EXC0014 revive: Annoying issue about not having a comment. The rare codebase has such comments
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ VERSION ?= "0.0.0-dev-$(shell git rev-parse --short HEAD)"
BUILD_DIR ?= $(CURDIR)
GO_LINKER_FLAGS=-ldflags "-s -w" -ldflags "-X main.version=$(VERSION)"

.PHONY: all build

.PHONY: all
all: test build

.PHONY: build
build:
@echo "$(OK_COLOR)==> Building (v${VERSION}) ... $(NO_COLOR)"
@CGO_ENABLED=0 go build $(GO_LINKER_FLAGS) -o "$(BUILD_DIR)/${NAME}"

.PHONY: test
test:
@echo "$(OK_COLOR)==> Running tests$(NO_COLOR)"
@CGO_ENABLED=0 go test -cover -coverprofile=coverage.txt -covermode=atomic ./...

.PHONY: lint
lint:
golangci-lint run --config=./.github/linters/.golangci.yml --fix
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/vgarvardt/rklotz

go 1.20
go 1.21

require (
github.com/asdine/storm/v3 v3.2.1
github.com/cappuccinotm/slogx v0.1.0
github.com/felixge/httpsnoop v1.0.4
github.com/go-chi/chi/v5 v5.0.10
github.com/gorilla/feeds v1.1.2
Expand All @@ -12,7 +13,6 @@ require (
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/yuin/goldmark v1.6.0
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.14.0
golang.org/x/sync v0.5.0
golang.org/x/text v0.14.0
Expand All @@ -24,7 +24,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.etcd.io/bbolt v1.3.4 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/Sereal/Sereal v0.0.0-20190618215532-0b8ac451a863 h1:BRrxwOZBolJN4gIwv
github.com/Sereal/Sereal v0.0.0-20190618215532-0b8ac451a863/go.mod h1:D0JMgToj/WdxCgd30Kc1UcA9E+WdZoJqeVOuYW7iTBM=
github.com/asdine/storm/v3 v3.2.1 h1:I5AqhkPK6nBZ/qJXySdI7ot5BlXSZ7qvDY1zAn5ZJac=
github.com/asdine/storm/v3 v3.2.1/go.mod h1:LEpXwGt4pIqrE/XcTvCnZHT5MgZCV6Ub9q7yQzOFWr0=
github.com/cappuccinotm/slogx v0.1.0 h1:xz+qAjeTv5X4O9k1QRUz9CwZS+XecZMcUbQyLyCiY7c=
github.com/cappuccinotm/slogx v0.1.0/go.mod h1:Q9lmOfumtErQpVTT5/3nKH++YRJMrYNvGe7Xkqnjr2Q=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -17,20 +19,24 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/feeds v1.1.2 h1:pxzZ5PD3RJdhFH2FsJJ4x6PqMqbgFk1+Vez4XWBW8Iw=
github.com/gorilla/feeds v1.1.2/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leekchan/gtf v0.0.0-20190214083521-5fba33c5b00b h1:ozQQA/k08pNmaav0AxE/EYzN4jvzvhD2idtcHcSAOSA=
github.com/leekchan/gtf v0.0.0-20190214083521-5fba33c5b00b/go.mod h1:thNruaSwydMhkQ8dXzapABF9Sc1Tz08ZBcDdgott9RA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMfPQbKMDE=
github.com/sethvargo/go-envconfig v0.9.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=
Expand All @@ -47,11 +53,6 @@ github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg=
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
Expand Down
9 changes: 4 additions & 5 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package loader

import (
"errors"
"log/slog"
"net/url"

"go.uber.org/zap"

"github.com/vgarvardt/rklotz/pkg/formatter"
"github.com/vgarvardt/rklotz/pkg/storage"
)
Expand All @@ -16,17 +15,17 @@ const (

var (
// ErrorUnknownLoaderType is the error returned when trying to instantiate a loader of unknown type
ErrorUnknownLoaderType = errors.New("Unknown loader type")
ErrorUnknownLoaderType = errors.New("unknown loader type")
)

// Loader is the interface for posts loader
type Loader interface {
// Load loads posts and saves them one by one in the storage
Load(storage storage.Storage) error
Load(s storage.Storage) error
}

// New returns new loader instance by type
func New(dsn string, logger *zap.Logger) (Loader, error) {
func New(dsn string, logger *slog.Logger) (Loader, error) {
postsURL, err := url.Parse(dsn)
if nil != err {
return nil, err
Expand Down
30 changes: 14 additions & 16 deletions pkg/loader/loader_file.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package loader

import (
"log/slog"
"os"
"path/filepath"

"go.uber.org/zap"

"github.com/vgarvardt/rklotz/pkg/formatter"
"github.com/vgarvardt/rklotz/pkg/model"
"github.com/vgarvardt/rklotz/pkg/storage"
Expand All @@ -15,39 +14,38 @@
type FileLoader struct {
path string
f formatter.Formatter
logger *zap.Logger
logger *slog.Logger
}

// NewFileLoader creates new FileLoader instance
func NewFileLoader(path string, f formatter.Formatter, logger *zap.Logger) (*FileLoader, error) {
func NewFileLoader(path string, f formatter.Formatter, logger *slog.Logger) (*FileLoader, error) {
return &FileLoader{path, f, logger}, nil
}

// Load loads posts and saves them one by one in the storage
func (l *FileLoader) Load(storage storage.Storage) error {
err := filepath.Walk(l.path, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
if nil != err {
return err
}
func (l *FileLoader) Load(s storage.Storage) error {
if err := filepath.Walk(l.path, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}

Check warning on line 30 in pkg/loader/loader_file.go

View check run for this annotation

Codecov / codecov/patch

pkg/loader/loader_file.go#L29-L30

Added lines #L29 - L30 were not covered by tests

l.logger.Debug("Loading post from file", zap.String("path", path))
if !f.IsDir() {
l.logger.Debug("Loading post from file", slog.String("path", path))
post, err := model.NewPostFromFile(l.path, path, l.f)
if err != nil {
return err
}

l.logger.Debug("Saving post to storage", zap.String("path", post.Path), zap.String("title", post.Title))
err = storage.Save(post)
l.logger.Debug("Saving post to storage", slog.String("path", post.Path), slog.String("title", post.Title))
err = s.Save(post)
if err != nil {
return err
}
}
return nil
})
if err != nil {
}); err != nil {

Check warning on line 46 in pkg/loader/loader_file.go

View check run for this annotation

Codecov / codecov/patch

pkg/loader/loader_file.go#L46

Added line #L46 was not covered by tests
return err
}

return storage.Finalize()
return s.Finalize()
}
16 changes: 9 additions & 7 deletions pkg/loader/loader_file_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package loader

import (
"log/slog"
"os"
"path/filepath"
"testing"

"github.com/cappuccinotm/slogx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/vgarvardt/rklotz/pkg/formatter"
"github.com/vgarvardt/rklotz/pkg/model"
Expand All @@ -31,15 +32,15 @@ func (s *mockStorage) Finalize() error {
return nil
}

func (s *mockStorage) FindByPath(path string) (*model.Post, error) {
func (s *mockStorage) FindByPath(string) (*model.Post, error) {
return nil, nil
}

func (s *mockStorage) ListAll(page int) ([]*model.Post, error) {
func (s *mockStorage) ListAll(int) ([]*model.Post, error) {
return nil, nil
}

func (s *mockStorage) ListTag(tag string, page int) ([]*model.Post, error) {
func (s *mockStorage) ListTag(string, int) ([]*model.Post, error) {
return nil, nil
}

Expand All @@ -55,7 +56,7 @@ func (s *mockStorage) Meta() *model.Meta {
}
}

func (s *mockStorage) TagMeta(tag string) *model.Meta {
func (s *mockStorage) TagMeta(string) *model.Meta {
return &model.Meta{
Posts: len(s.saveCallParams),
PerPage: 0,
Expand All @@ -72,9 +73,10 @@ func TestFileLoader_Load(t *testing.T) {

storage := &mockStorage{saveCallResult: []error{nil, nil, nil}}

logger := slog.New(slogx.TestHandler(t))
f := formatter.New()

fileLoader, err := NewFileLoader(postsBasePath, f, zap.NewNop())
fileLoader, err := NewFileLoader(postsBasePath, f, logger)
require.NoError(t, err)

err = fileLoader.Load(storage)
Expand All @@ -97,7 +99,7 @@ func TestFileLoader_Load(t *testing.T) {
title: "Post With Teaser",
}} {
t.Run(p.path, func(t *testing.T) {
found := false
var found bool
for _, post := range storage.saveCallParams {
if post.Path == p.path {
found = true
Expand Down
Loading
Loading