Skip to content

Commit

Permalink
dependabot-1 Major linting fixes, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bdebyl committed Jul 15, 2022
1 parent 0be1fec commit 8b88665
Show file tree
Hide file tree
Showing 35 changed files with 170 additions and 107 deletions.
3 changes: 2 additions & 1 deletion .errcheck_excludes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
(github.com/go-kit/kit/log.Logger).Log
(github.com/go-kit/log.Logger).Log
b.logger.Log
9 changes: 7 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ run:
tests: false
modules-download-mode: vendor
timeout: 5m
skip-dirs:
- test

linters:
enable-all: true
disable:
- exhaustivestruct
- exhaustruct
- goerr113
- ireturn
- maintidx
- varnamelen

linters-settings:
Expand All @@ -20,5 +26,4 @@ linters-settings:
line-length: 120
funlen:
lines: 70
statements: 40

statements: 40
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test-e2e: $(GOTEST) ; $(info $(M) running test-e2e )
lint: ## Runs golangci-lint analysis
lint:
# Check .golangci.yml for configuration
$(Q) $(GOLANGCI_LINT) run -v --enable-all --skip-dirs tmp -c .golangci.yml
$(Q) $(GOLANGCI_LINT) run -v --skip-dirs tmp -c .golangci.yml

.PHONY: fix
fix: ## Runs golangci-lint fix
Expand Down
6 changes: 3 additions & 3 deletions archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"compress/flate"
"io"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/meltwater/drone-cache/archive/gzip"
"github.com/meltwater/drone-cache/archive/tar"
"github.com/meltwater/drone-cache/archive/zstd"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)

const (
Expand Down Expand Up @@ -51,6 +50,7 @@ func FromFormat(logger log.Logger, root string, format string, opts ...Option) A
return zstd.New(logger, root, options.skipSymlinks, options.compressionLevel)
default:
level.Error(logger).Log("msg", "unknown archive format", "format", format)

return tar.New(logger, root, options.skipSymlinks) // DefaultArchiveFormat
}
}
19 changes: 14 additions & 5 deletions archive/gzip/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"fmt"
"io"

"github.com/go-kit/log"
"github.com/meltwater/drone-cache/archive/tar"
"github.com/meltwater/drone-cache/internal"

"github.com/go-kit/kit/log"
)

// Archive implements archive for gzip.
Expand All @@ -34,17 +33,27 @@ func (a *Archive) Create(srcs []string, w io.Writer) (int64, error) {

defer internal.CloseWithErrLogf(a.logger, gw, "gzip writer")

return tar.New(a.logger, a.root, a.skipSymlinks).Create(srcs, gw)
wBytes, err := tar.New(a.logger, a.root, a.skipSymlinks).Create(srcs, gw)
if err != nil {
return 0, fmt.Errorf("writing create archive bytes: %w", err)
}

return wBytes, nil
}

// Extract reads content from the given archive reader and restores it to the destination, returns written bytes.
func (a *Archive) Extract(dst string, r io.Reader) (int64, error) {
gr, err := gzip.NewReader(r)
if err != nil {
return 0, err
return 0, fmt.Errorf("create archive extractor: %w", err)
}

defer internal.CloseWithErrLogf(a.logger, gr, "gzip reader")

return tar.New(a.logger, a.root, a.skipSymlinks).Extract(dst, gr)
eBytes, err := tar.New(a.logger, a.root, a.skipSymlinks).Extract(dst, gr)
if err != nil {
return 0, fmt.Errorf("extracting archive bytes: %w", err)
}

return eBytes, nil
}
2 changes: 1 addition & 1 deletion archive/gzip/gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
"testing"

"github.com/go-kit/kit/log"
"github.com/go-kit/log"

"github.com/meltwater/drone-cache/archive/tar"
"github.com/meltwater/drone-cache/test"
Expand Down
17 changes: 8 additions & 9 deletions archive/tar/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"strings"

"github.com/go-kit/log"

"github.com/meltwater/drone-cache/internal"
)

const defaultDirPermission = 0755
const defaultDirPermission = 0o755

var (
// ErrSourceNotReachable means that given source is not reachable.
Expand Down Expand Up @@ -57,7 +56,7 @@ func (a *Archive) Create(srcs []string, w io.Writer) (int64, error) {
return written, nil
}

// nolint: lll
// nolint: lll, cyclop
func writeToArchive(tw *tar.Writer, root string, skipSymlinks bool, written *int64) func(string, os.FileInfo, error) error {
return func(path string, fi os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -146,7 +145,7 @@ func createSymlinkHeader(fi os.FileInfo, path string) (*tar.Header, error) {
return h, nil
}

func writeFileToArchive(tw io.Writer, path string) (n int64, err error) {
func writeFileToArchive(tw io.Writer, path string) (int64, error) {
f, err := os.Open(path)
if err != nil {
return 0, fmt.Errorf("open file <%s>, %w", path, err)
Expand All @@ -163,6 +162,7 @@ func writeFileToArchive(tw io.Writer, path string) (n int64, err error) {
}

// Extract reads content from the given archive reader and restores it to the destination, returns written bytes.
// nolint: cyclop
func (a *Archive) Extract(dst string, r io.Reader) (int64, error) {
var (
written int64
Expand All @@ -173,7 +173,7 @@ func (a *Archive) Extract(dst string, r io.Reader) (int64, error) {
h, err := tr.Next()

switch {
case err == io.EOF: // if no more files are found return
case errors.Is(err, io.EOF): // if no more files are found return
return written, nil
case err != nil: // return any other error
return written, fmt.Errorf("tar reader <%v>, %w", err, ErrArchiveNotReadable)
Expand Down Expand Up @@ -241,7 +241,7 @@ func extractDir(h *tar.Header, target string) error {
return nil
}

func extractRegular(h *tar.Header, tr io.Reader, target string) (n int64, err error) {
func extractRegular(h *tar.Header, tr io.Reader, target string) (int64, error) {
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(h.Mode))
if err != nil {
return 0, fmt.Errorf("open extracted file for writing <%s>, %w", target, err)
Expand Down Expand Up @@ -282,9 +282,8 @@ func extractLink(h *tar.Header, target string) error {
}

func unlink(path string) error {
_, err := os.Lstat(path)
if err == nil {
return os.Remove(path)
if _, err := os.Lstat(path); err == nil {
return fmt.Errorf("error with unlinking: %w", os.Remove(path))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion archive/tar/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/meltwater/drone-cache/test"

"github.com/go-kit/kit/log"
"github.com/go-kit/log"
)

var (
Expand Down
22 changes: 15 additions & 7 deletions archive/zstd/zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"fmt"
"io"

"github.com/go-kit/log"
"github.com/klauspost/compress/zstd"

"github.com/meltwater/drone-cache/archive/tar"
"github.com/meltwater/drone-cache/internal"

"github.com/go-kit/kit/log"
)

// Archive implements archive for zstd.
Expand All @@ -30,22 +28,32 @@ func New(logger log.Logger, root string, skipSymlinks bool, compressionLevel int
func (a *Archive) Create(srcs []string, w io.Writer) (int64, error) {
zw, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(a.compressionLevel)))
if err != nil {
return 0, fmt.Errorf("create archive writer, %w", err)
return 0, fmt.Errorf("zstd create archive writer, %w", err)
}

defer internal.CloseWithErrLogf(a.logger, zw, "zstd writer")

return tar.New(a.logger, a.root, a.skipSymlinks).Create(srcs, zw)
wBytes, err := tar.New(a.logger, a.root, a.skipSymlinks).Create(srcs, zw)
if err != nil {
return 0, fmt.Errorf("zstd create archive, %w", err)
}

return wBytes, nil
}

// Extract reads content from the given archive reader and restores it to the destination, returns written bytes.
func (a *Archive) Extract(dst string, r io.Reader) (int64, error) {
zr, err := zstd.NewReader(r)
if err != nil {
return 0, err
return 0, fmt.Errorf("zstd create extract archive reader, %w", err)
}

defer internal.CloseWithErrLogf(a.logger, zr.IOReadCloser(), "zstd reader")

return tar.New(a.logger, a.root, a.skipSymlinks).Extract(dst, zr)
eBytes, err := tar.New(a.logger, a.root, a.skipSymlinks).Extract(dst, zr)
if err != nil {
return 0, fmt.Errorf("zstd extract archive, %w", err)
}

return eBytes, nil
}
2 changes: 1 addition & 1 deletion archive/zstd/zstd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"testing"

"github.com/go-kit/kit/log"
"github.com/go-kit/log"

"github.com/meltwater/drone-cache/archive/tar"
"github.com/meltwater/drone-cache/test"
Expand Down
3 changes: 1 addition & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ package cache
import (
"time"

"github.com/go-kit/kit/log"

"github.com/go-kit/log"
"github.com/meltwater/drone-cache/archive"
"github.com/meltwater/drone-cache/key"
"github.com/meltwater/drone-cache/storage"
Expand Down
5 changes: 2 additions & 3 deletions cache/flusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"fmt"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/meltwater/drone-cache/storage"
"github.com/meltwater/drone-cache/storage/backend"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)

type flusher struct {
Expand Down
11 changes: 5 additions & 6 deletions cache/rebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"sync"
"time"

"github.com/dustin/go-humanize"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/meltwater/drone-cache/archive"
"github.com/meltwater/drone-cache/internal"
"github.com/meltwater/drone-cache/key"
"github.com/meltwater/drone-cache/storage"

"github.com/dustin/go-humanize"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)

type rebuilder struct {
Expand Down Expand Up @@ -133,7 +132,7 @@ func (r rebuilder) rebuild(src, dst string) error {
level.Error(r.logger).Log("msg", "pr close", "err", err)
}

return err
return fmt.Errorf("rebuilder rebuild put file, %w", err)
}

level.Debug(r.logger).Log(
Expand Down Expand Up @@ -165,5 +164,5 @@ func (r rebuilder) generateKey(parts ...string) (string, error) {
}
}

return "", err
return "", fmt.Errorf("rebuilder generate key, %w", err)
}
7 changes: 3 additions & 4 deletions cache/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"sync"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/meltwater/drone-cache/archive"
"github.com/meltwater/drone-cache/internal"
"github.com/meltwater/drone-cache/key"
Expand Down Expand Up @@ -134,5 +133,5 @@ func (r restorer) generateKey(parts ...string) (string, error) {
}
}

return "", err
return "", fmt.Errorf("restorer generate key, %w", err)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/Azure/azure-storage-blob-go v0.15.0
github.com/aws/aws-sdk-go v1.44.55
github.com/dustin/go-humanize v1.0.0
github.com/go-kit/kit v0.12.0
github.com/go-kit/log v0.2.1
github.com/google/go-cmp v0.5.8
github.com/klauspost/compress v1.15.8
Expand Down Expand Up @@ -40,7 +39,7 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-kit/kit v0.12.0 h1:e4o3o3IsBfAKQh5Qbbiqyfu97Ku7jrO/JbohvztANh4=
github.com/go-kit/kit v0.12.0/go.mod h1:lHd+EkCZPIwYItmGDDRdhinkzX2A1sj+M9biaEaizzs=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
Expand Down Expand Up @@ -476,8 +474,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
2 changes: 1 addition & 1 deletion internal/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ func closeIo(closer io.Closer) error {
return nil
}

return err
return fmt.Errorf("error closing io.Closer: %w", err)
}
3 changes: 2 additions & 1 deletion internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewLogger(logLevel, logFormat, name string) log.Logger {

logger = level.NewFilter(logger, lvl)
logger = log.With(logger, "name", name)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)

return log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return logger
}
Loading

0 comments on commit 8b88665

Please sign in to comment.