Skip to content

Commit

Permalink
fix: fixed linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarvardt committed Nov 11, 2023
1 parent b261028 commit 75e6d30
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 36 deletions.
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
2 changes: 1 addition & 1 deletion pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
// 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
Expand Down
6 changes: 3 additions & 3 deletions pkg/loader/loader_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewFileLoader(path string, f formatter.Formatter, logger *slog.Logger) (*Fi
}

// Load loads posts and saves them one by one in the storage
func (l *FileLoader) Load(storage storage.Storage) error {
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
Expand All @@ -37,7 +37,7 @@ func (l *FileLoader) Load(storage storage.Storage) error {
}

l.logger.Debug("Saving post to storage", slog.String("path", post.Path), slog.String("title", post.Title))
err = storage.Save(post)
err = s.Save(post)
if err != nil {
return err
}
Expand All @@ -47,5 +47,5 @@ func (l *FileLoader) Load(storage storage.Storage) error {
return err
}

return storage.Finalize()
return s.Finalize()
}
4 changes: 2 additions & 2 deletions pkg/server/handler/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Feed struct {
}

// NewFeed creates new Feed instance
func NewFeed(storage storage.Storage, renderer renderer.Renderer) *Feed {
return &Feed{storage, renderer}
func NewFeed(s storage.Storage, r renderer.Renderer) *Feed {
return &Feed{s, r}
}

// Atom is the HTTP handler for Atom feed
Expand Down
5 changes: 2 additions & 3 deletions pkg/server/handler/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Posts struct {
}

// NewPosts creates new Posts instance
func NewPosts(storage storage.Storage, renderer renderer.Renderer) *Posts {
return &Posts{storage, renderer}
func NewPosts(s storage.Storage, r renderer.Renderer) *Posts {
return &Posts{s, r}
}

// Front is the HTTP handler for the front page with post list
Expand Down Expand Up @@ -72,7 +72,6 @@ func (h *Posts) Post(w http.ResponseWriter, r *http.Request) {
}

h.renderer.Render(w, http.StatusOK, renderer.NewData(r, "post.tpl", renderer.D{"post": post}))

}

func (h *Posts) getPageFromURL(r *http.Request) int {
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/renderer/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func (r *HTML) initTemplates() error {
func (r *HTML) getPartials(templatesPath, theme, uiAbout string) ([]string, error) {
var partials []string

walkFn := func(path string, f os.FileInfo, err error) error {
if nil == err && !f.IsDir() && !strings.HasSuffix(path, "about.tpl") {
partials = append(partials, path)
walkFn := func(fPath string, f os.FileInfo, err error) error {
if nil == err && !f.IsDir() && !strings.HasSuffix(fPath, "about.tpl") {
partials = append(partials, fPath)
}
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// D is the syntax-sugar type for map
type D map[string]interface{}
type D map[string]any

// Data is the container type for renderer data
type Data struct {
Expand All @@ -30,7 +30,7 @@ func NewData(r *http.Request, template string, data D) *Data {
}

// Set sets named value to the data container
func (d *Data) Set(key string, value interface{}) *Data {
func (d *Data) Set(key string, value any) *Data {

Check warning on line 33 in pkg/server/renderer/renderer.go

View check run for this annotation

Codecov / codecov/patch

pkg/server/renderer/renderer.go#L33

Added line #L33 was not covered by tests
d.m.Lock()
defer d.m.Unlock()

Expand Down
6 changes: 3 additions & 3 deletions pkg/server/web/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func ServeStatic(r chi.Router, cfgHTTP HTTPConfig, theme string) {
}

// ListenAndServe launches web server that listens to HTTP(S) requests
func ListenAndServe(ctx context.Context, handler chi.Router, cfgSSL SSLConfig, cfgHTTP HTTPConfig, logger *slog.Logger) error {
func ListenAndServe(ctx context.Context, router chi.Router, cfgSSL SSLConfig, cfgHTTP HTTPConfig, logger *slog.Logger) error {
if !cfgSSL.Enabled {
server := &http.Server{
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Addr: fmt.Sprintf(":%d", cfgHTTP.Port),
Handler: handler,
Handler: router,
}

logger.Info("Running HTTP server...", slog.String("address", server.Addr))
Expand All @@ -87,7 +87,7 @@ func ListenAndServe(ctx context.Context, handler chi.Router, cfgSSL SSLConfig, c
ReadHeaderTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Addr: fmt.Sprintf(":%d", cfgSSL.Port),
Handler: handler,
Handler: router,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
Expand Down
32 changes: 19 additions & 13 deletions pkg/storage/storage_boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ import (
"github.com/vgarvardt/rklotz/pkg/model"
)

func getRandomHash(length int) string {
func getRandomHash(t *testing.T, length int) string {
t.Helper()

hasher := md5.New()
hasher.Write([]byte(time.Now().Format(time.RFC3339Nano)))
_, err := hasher.Write([]byte(time.Now().Format(time.RFC3339Nano)))
require.NoError(t, err)

return hex.EncodeToString(hasher.Sum(nil))[:length]
}

func getFilePath() string {
return fmt.Sprintf("/tmp/rklotz-test.%s.db", getRandomHash(5))
func getFilePath(t *testing.T) string {
t.Helper()

return fmt.Sprintf("/tmp/rklotz-test.%s.db", getRandomHash(t, 5))
}

func fileExists(path string) bool {
Expand All @@ -32,7 +38,7 @@ func fileExists(path string) bool {
}

func TestNewBoltDBStorage(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 10)
require.NoError(t, err)
assert.Equal(t, dbFilePath, storage.path)
Expand All @@ -46,7 +52,7 @@ func TestNewBoltDBStorage(t *testing.T) {
}

func TestBoltDBStorage_Finalize(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 10)
require.NoError(t, err)
defer func() {
Expand Down Expand Up @@ -94,7 +100,7 @@ func loadTestPosts(t *testing.T, storage Storage) {
}

func TestBoltDBStorage_FindByPath(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 10)
require.NoError(t, err)
defer func() {
Expand All @@ -119,7 +125,7 @@ func TestBoltDBStorage_FindByPath(t *testing.T) {
}

func TestBoltDBStorage_ListAll_10(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 10)
require.NoError(t, err)
defer func() {
Expand All @@ -143,7 +149,7 @@ func TestBoltDBStorage_ListAll_10(t *testing.T) {
}

func TestBoltDBStorage_ListAll_1(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 1)
require.NoError(t, err)
defer func() {
Expand All @@ -170,7 +176,7 @@ func TestBoltDBStorage_ListAll_1(t *testing.T) {
}

func TestBoltDBStorage_ListTag_10(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 10)
require.NoError(t, err)
defer func() {
Expand All @@ -196,7 +202,7 @@ func TestBoltDBStorage_ListTag_10(t *testing.T) {
}

func TestBoltDBStorage_ListTag_1(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 1)
require.NoError(t, err)
defer func() {
Expand Down Expand Up @@ -225,7 +231,7 @@ func TestBoltDBStorage_ListTag_1(t *testing.T) {
}

func TestBoltDBStorage_ListTag_ErrorNotFound(t *testing.T) {
dbFilePath := getFilePath()
dbFilePath := getFilePath(t)
storage, err := NewBoltDBStorage(dbFilePath, 1)
require.NoError(t, err)
defer func() {
Expand All @@ -235,7 +241,7 @@ func TestBoltDBStorage_ListTag_ErrorNotFound(t *testing.T) {

loadTestPosts(t, storage)

tag := getRandomHash(10)
tag := getRandomHash(t, 10)
_, err = storage.ListTag(tag, 0)
require.Error(t, err)
assert.Equal(t, ErrorNotFound, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/storage_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func (s *MemoryStorage) Save(post *model.Post) error {
func (s *MemoryStorage) Finalize() error {
s.postsList = make([]*model.Post, s.postsCount)
i := 0
s.posts.Range(func(path, post interface{}) bool {
s.posts.Range(func(path, post any) bool {
s.postsList[i] = post.(*model.Post)
i++
return true
})
sort.Sort(sort.Reverse(s.postsList))

s.tagsList.Range(func(tag, tagSlice interface{}) bool {
s.tagsList.Range(func(tag, tagSlice any) bool {
sort.Sort(sort.Reverse(tagSlice.(postSlice)))
return true
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/storage_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestMemoryStorage_ListTag_ErrorNotFound(t *testing.T) {

loadTestPosts(t, storage)

tag := getRandomHash(10)
tag := getRandomHash(t, 10)
_, err = storage.ListTag(tag, 0)
require.Error(t, err)
assert.Equal(t, ErrorNotFound, err)
Expand Down
5 changes: 4 additions & 1 deletion pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewStorage(t *testing.T) {
hasher := md5.New()
hasher.Write([]byte(time.Now().Format(time.RFC3339Nano)))
_, err := hasher.Write([]byte(time.Now().Format(time.RFC3339Nano)))
require.NoError(t, err)

dbFilePath := fmt.Sprintf("/tmp/rklotz-test.%s.db", hex.EncodeToString(hasher.Sum(nil))[:5])

boltDBStorage, err := NewStorage("boltdb://"+dbFilePath, 10)
Expand Down

0 comments on commit 75e6d30

Please sign in to comment.