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

Add golangci lint #105

Merged
merged 18 commits into from
Mar 11, 2024
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
55 changes: 55 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: golangci-lint
on:
push:
branches:
- master
- main
pull_request:

permissions:
contents: read
pull-requests: read
checks: write

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.56

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
# args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true, then all caching functionality will be completely disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
96 changes: 96 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
run:
timeout: 5m

linters-settings:
errcheck:
check-type-assertions: true

funlen:
lines: 100
statements: 50

gci:
sections:
- standard # Standard section: captures all standard packages.
- prefix(github.com/tobischo/gokeepasslib) # Custom section: groups all imports with the specified Prefix.
- default # Default section: contains all imports that could not be matched to another section type.

skip-generated: false
custom-order: true

gocognit:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 10

gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile

govet:
settings:
shadow:
strict: true
enable-all: true

nakedret:
max-func-lines: 0 # Do not use naked returns

lll:
line-length: 100
tab-width: 2

godot:
period: false

linters:
enable-all: true
disable:
- depguard
- nlreturn
- paralleltest
- tagliatelle
- thelper
- wsl
- forbidigo
- varnamelen
- exhaustruct
- errname
- gochecknoglobals
- testpackage
- funlen
- cyclop
- gofumpt
- deadcode
- interfacer
- maligned
- nosnakecase
- scopelint
- structcheck
- varcheck
- wrapcheck
- stylecheck
- testableexamples
- ifshort
- errcheck
- nestif
- exhaustivestruct
- govet
- musttag
- nilnil
- gomnd
- errorlint
- revive
- unconvert
- gocritic
- gocognit
- dupl
- goconst

issues:
max-ame-issues: 50

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21.6
FROM golang:1.22.0

# Don't run tests as root so we can play with permissions
RUN useradd --create-home --user-group app
Expand Down
17 changes: 10 additions & 7 deletions binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"

w "github.com/tobischo/gokeepasslib/v3/wrappers"
)
Expand All @@ -18,7 +18,7 @@ type Binaries []Binary

// Binary stores a binary found in the metadata header of a database
type Binary struct {
ID int `xml:"ID,attr"` // Index of binary (Manually counted on KDBX v4)
ID int `xml:"ID,attr"` // Index (Manually counted on KDBX v4)
MemoryProtection byte `xml:"-"` // Memory protection flag (Only KDBX v4)
Content []byte `xml:",innerxml"` // Binary content
Compressed w.BoolWrapper `xml:"Compressed,attr"` // Compressed flag (Only KDBX v3.1)
Expand All @@ -43,8 +43,10 @@ func (bs Binaries) Find(id int) *Binary {
return nil
}

// Deprecated: Find returns a reference to a binary in the database db with the same id as br, or nil if none is found
// Note: this function should not be used directly, use `Database.FindBinary(id int) *Binary` instead
// Deprecated: Find returns a reference to a binary in the database db
// with the same id as br, or nil if none is found
// Note: this function should not be used directly, use `Database.FindBinary(id int) *Binary`
// instead
func (br *BinaryReference) Find(db *Database) *Binary {
return db.getBinaries().Find(br.Value.ID)
}
Expand All @@ -67,7 +69,8 @@ func WithKDBXv31Binary(binary *Binary) {
}

// Deprecated: Add appends binary data to the slice
// Note: this function should not be used directly, use `Database.AddBinary(c []byte) *Binary` instead
// Note: this function should not be used directly,
// use `Database.AddBinary(c []byte) *Binary` instead
func (bs *Binaries) Add(c []byte, options ...BinaryOption) *Binary {
for _, binary := range *bs {
if bytes.Equal(binary.Content, c) {
Expand Down Expand Up @@ -109,8 +112,8 @@ func (b Binary) GetContentBytes() ([]byte, error) {
return nil, err
}
defer reader.Close()
bts, err := ioutil.ReadAll(reader)
if err != nil && err != io.ErrUnexpectedEOF {
bts, err := io.ReadAll(reader)
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
return nil, err
}

Expand Down
46 changes: 36 additions & 10 deletions binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gokeepasslib
import (
"bytes"
"crypto/rand"
"fmt"
"strconv"
"testing"
)

Expand All @@ -30,15 +30,21 @@ func TestBinaryKDBXv31(t *testing.T) {
t.Fatalf("Binary Reference ID is incorrect. Should be 4, was %d", references[0].Value.ID)
}
if data, _ := db.FindBinary(references[0].Value.ID).GetContentBytes(); string(data) != "test" {
t.Fatalf("Binary Reference GetContentBytes is incorrect. Should be `test`, was '%s'", string(data))
t.Fatalf(
"Binary Reference GetContentBytes is incorrect. Should be `test`, was '%s'",
string(data),
)
}
if str, _ := db.FindBinary(references[0].Value.ID).GetContentString(); str != "test" {
t.Fatalf("Binary Reference GetContentString is incorrect. Should be `test`, was '%s'", str)
}

found := db.FindBinary(binary2.ID)
if data, _ := found.GetContentBytes(); string(data) != "Hello world!" {
t.Fatalf("Binary content from FindBinary is incorrect. Should be `Hello world!`, was '%s'", string(data))
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `Hello world!`, was '%s'",
string(data),
)
}
if str, _ := found.GetContentString(); str != "Hello world!" {
t.Fatalf("Binary content from FindBinary is incorrect. Should be `Hello world!`, was '%s'", str)
Expand Down Expand Up @@ -81,7 +87,7 @@ func TestBinaryKDBXv31CleanBinaries(t *testing.T) {
count := 5

for i := 0; i < count; i++ {
str := "test " + fmt.Sprint(i)
str := "test " + strconv.Itoa(i)
expectedContent = append(expectedContent, str)
expected = append(expected, db.AddBinary([]byte(str)))
}
Expand All @@ -101,7 +107,11 @@ func TestBinaryKDBXv31CleanBinaries(t *testing.T) {
for i := 0; i < count; i++ {
found := db.FindBinary(binaries[i].Value.ID)
if data, _ := found.GetContentString(); string(data) != expectedContent[i] {
t.Fatalf("Binary content from FindBinary is incorrect. Should be `%s`, was '%s'", expectedContent[i], string(data))
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
expectedContent[i],
string(data),
)
}
}

Expand Down Expand Up @@ -139,7 +149,11 @@ func TestBinaryKDBXv31CleanBinaries(t *testing.T) {
t.Fatalf("Binary (ID=%d) not found", i)
}
if data, _ := found.GetContentBytes(); string(data) != expectedContent[i] {
t.Fatalf("Binary content from FindBinary is incorrect. Should be `%s`, was '%s'", expectedContent[i], string(data))
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
expectedContent[i],
string(data),
)
}

ref := db.Content.Root.Groups[0].Entries[0].Binaries[i]
Expand All @@ -156,7 +170,7 @@ func TestBinaryKDBXv4CleanBinaries(t *testing.T) {
count := 5

for i := 0; i < count; i++ {
expected = append(expected, db.AddBinary([]byte("test "+fmt.Sprint(i))))
expected = append(expected, db.AddBinary([]byte("test "+strconv.Itoa(i))))
}

if len(db.Content.InnerHeader.Binaries) != count {
Expand All @@ -174,7 +188,11 @@ func TestBinaryKDBXv4CleanBinaries(t *testing.T) {
for i := 0; i < count; i++ {
found := db.FindBinary(binaries[i].Value.ID)
if data, _ := found.GetContentBytes(); string(data) != string(expected[i].Content) {
t.Fatalf("Binary content from FindBinary is incorrect. Should be `%s`, was '%s'", string(expected[i].Content), string(data))
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
string(expected[i].Content),
string(data),
)
}
}

Expand All @@ -200,7 +218,11 @@ func TestBinaryKDBXv4CleanBinaries(t *testing.T) {
db.UnlockProtectedEntries()

if len(db.Content.InnerHeader.Binaries) != len(expected) {
t.Fatalf("Expected %d binary elements, found %d", len(expected), len(db.Content.InnerHeader.Binaries))
t.Fatalf(
"Expected %d binary elements, found %d",
len(expected),
len(db.Content.InnerHeader.Binaries),
)
}

for i := 0; i < len(expected); i++ {
Expand All @@ -209,7 +231,11 @@ func TestBinaryKDBXv4CleanBinaries(t *testing.T) {
t.Fatalf("Binary (ID=%d) not found", i)
}
if data, _ := found.GetContentBytes(); string(data) != string(expected[i].Content) {
t.Fatalf("Binary content from FindBinary is incorrect. Should be `%s`, was '%s'", string(expected[i].Content), string(data))
t.Fatalf(
"Binary content from FindBinary is incorrect. Should be `%s`, was '%s'",
string(expected[i].Content),
string(data),
)
}

ref := db.Content.Root.Groups[0].Entries[0].Binaries[i]
Expand Down
Loading
Loading