Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed May 7, 2024
1 parent 819bdf9 commit 6b5f900
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions coalesce/coalesce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestNotNil(t *testing.T) {
require.Nil(t, got)
return
}

require.NotNil(t, got)
assert.Equal(t, *tc.want, *got)
})
Expand Down
6 changes: 3 additions & 3 deletions csdaemon/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func NotifySystemd(log logrus.FieldLogger) error {
}

const (
Ready = daemon.SdNotifyReady
Ready = daemon.SdNotifyReady
Reloading = daemon.SdNotifyReloading
Stopping = daemon.SdNotifyStopping
Watchdog = daemon.SdNotifyWatchdog
Stopping = daemon.SdNotifyStopping
Watchdog = daemon.SdNotifyWatchdog
)

// Notify systemd that the service is ready.
Expand Down
1 change: 1 addition & 0 deletions csstring/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package csstring_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/crowdsecurity/go-cs-lib/csstring"
Expand Down
3 changes: 3 additions & 0 deletions ptr/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func OrEmpty[T any](t *T) T {
if t != nil {
return *t
}

var empty T

return empty
}

Expand All @@ -38,6 +40,7 @@ func OrDefault[T any](t *T, def T) T {
if t != nil {
return *t
}

return def
}

Expand Down
3 changes: 2 additions & 1 deletion slicetools/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/crowdsecurity/go-cs-lib/slicetools"
)

Expand All @@ -14,7 +15,7 @@ func TestChunks(t *testing.T) {
chunkSize int
expected [][]int
}{
{ "empty slice, chunk size 2", []int{}, 2, [][]int{}},
{"empty slice, chunk size 2", []int{}, 2, [][]int{}},
{"1 element, chunk size 2", []int{1}, 2, [][]int{{1}}},
{"empty slice, chunk size 0", []int{}, 0, [][]int{}},
{"5 elements, chunk size 2", []int{1, 2, 3, 4, 5}, 2, [][]int{{1, 2}, {3, 4}, {5}}},
Expand Down
6 changes: 6 additions & 0 deletions slicetools/deduplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ func UniqueCopy[T comparable](slice []T) []T {
if slice == nil {
return nil
}

seen := make(map[T]struct{})
ret := make([]T, 0, len(slice))

for _, value := range slice {
if _, ok := seen[value]; !ok {
seen[value] = struct{}{}

ret = append(ret, value)
}
}

return ret
}

Expand All @@ -24,8 +28,10 @@ func Deduplicate[T comparable](slice []T) []T {
if slice == nil {
return nil
}

seen := make(map[T]struct{})
j := 0

for _, value := range slice {
if _, ok := seen[value]; !ok {
seen[value] = struct{}{}
Expand Down
4 changes: 4 additions & 0 deletions slicetools/deduplicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slicetools

import (
"testing"

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

Expand All @@ -24,15 +25,18 @@ func TestUniqueIntegers(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
got := UniqueCopy(tc.slice)
assert.Equal(t, tc.want, got)

if len(got) > 0 {
assert.NotEqual(t, got, tc.slice[:len(got)], "UniqueCopy should not modify the original slice!")
}

got = Deduplicate(tc.slice)
assert.Equal(t, tc.want, got)

if len(got) > 0 {
assert.Equal(t, got, tc.slice[:len(got)], "Deduplicate should modify the original slice!")
}

for i := len(got); i < len(tc.slice); i++ {
assert.Equal(t, 0, tc.slice[i], "Deduplicate should zero out the remaining elements!")
}
Expand Down
3 changes: 2 additions & 1 deletion trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (
// (i.e. config_paths.data_dir should be persistent even within containers)
// If not called, the default is /tmp or equivalent
func Init(dir string) error {
err := os.MkdirAll(dir, 0700)
err := os.MkdirAll(dir, 0o700)
if err != nil {
return err
}

keeper.dir = dir

return nil
Expand Down
6 changes: 2 additions & 4 deletions yamlpatch/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,22 @@ import (
type Patcher struct {
BaseFilePath string
PatchFilePath string
quiet bool
quiet bool
}

func NewPatcher(filePath string, suffix string) *Patcher {
return &Patcher{
BaseFilePath: filePath,
PatchFilePath: filePath + suffix,
quiet: false,
quiet: false,
}
}


// SetQuiet sets the quiet flag, which will log as DEBUG_LEVEL instead of INFO
func (p *Patcher) SetQuiet(quiet bool) {
p.quiet = quiet
}


// read a single YAML file, check for errors (the merge package doesn't) then return the content as bytes.
func readYAML(filePath string) ([]byte, error) {
content, err := os.ReadFile(filePath)
Expand Down
2 changes: 2 additions & 0 deletions yamlpatch/patcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func TestMergedPatchContent(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

dirPath, err := os.MkdirTemp("", "yamlpatch")
require.NoError(t, err)

Expand Down Expand Up @@ -289,6 +290,7 @@ func TestPrependedPatchContent(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

dirPath, err := os.MkdirTemp("", "yamlpatch")
require.NoError(t, err)

Expand Down

0 comments on commit 6b5f900

Please sign in to comment.