diff --git a/coalesce/coalesce.go b/coalesce/coalesce.go index 6805239..a4e6da2 100644 --- a/coalesce/coalesce.go +++ b/coalesce/coalesce.go @@ -7,6 +7,7 @@ func String(s ...string) string { return v } } + return "" } @@ -17,6 +18,7 @@ func Int(s ...int) int { return v } } + return 0 } @@ -27,6 +29,7 @@ func NotNil[T any](args ...*T) *T { return arg } } + return nil } @@ -40,6 +43,7 @@ func NotEmptyOrNil[T comparable](args ...*T) *T { return arg } } + return nil } */ diff --git a/csdaemon/systemd.go b/csdaemon/systemd.go index 7bd40df..24fe495 100644 --- a/csdaemon/systemd.go +++ b/csdaemon/systemd.go @@ -26,9 +26,11 @@ func Notify(state string, log logrus.FieldLogger) error { log.Debugf("Systemd notified: %s", state) return err } + if err != nil { log.Errorf("Failed to notify systemd: %v", err) return err } + return nil } diff --git a/csstring/interpolate.go b/csstring/interpolate.go index 8af0859..6000524 100644 --- a/csstring/interpolate.go +++ b/csstring/interpolate.go @@ -14,8 +14,8 @@ func Interpolate(s string, data interface{}) (string, error) { } var b strings.Builder - err = tmpl.Execute(&b, data) - if err != nil { + + if err := tmpl.Execute(&b, data); err != nil { return "", err } diff --git a/cstest/errors.go b/cstest/errors.go index 51390e3..e2918c9 100644 --- a/cstest/errors.go +++ b/cstest/errors.go @@ -33,7 +33,9 @@ func AssertErrorMessage(t *testing.T, err error, expectedErr string) { if err != nil { errmsg = err.Error() } + assert.Equal(t, expectedErr, errmsg) + return } @@ -59,7 +61,9 @@ func RequireErrorMessage(t *testing.T, err error, expectedErr string) { if err != nil { errmsg = err.Error() } + require.Equal(t, expectedErr, errmsg) + return } diff --git a/slicetools/chunk.go b/slicetools/chunk.go index 71fdd0a..20e5c12 100644 --- a/slicetools/chunk.go +++ b/slicetools/chunk.go @@ -7,10 +7,12 @@ func Chunks[T any](items []T, chunkSize int) [][]T { if len(items) == 0 { return [][]T{} } + return [][]T{items} } var ret [][]T + chunk := make([]T, 0, chunkSize) for _, v := range items { @@ -20,8 +22,10 @@ func Chunks[T any](items []T, chunkSize int) [][]T { chunk = nil } } + if len(chunk) > 0 { ret = append(ret, chunk) } + return ret } diff --git a/trace/trace.go b/trace/trace.go index 8503cf2..ef390dc 100644 --- a/trace/trace.go +++ b/trace/trace.go @@ -15,21 +15,26 @@ func WriteStackTrace(iErr interface{}) string { if err != nil { log.Fatal(err) } - if _, err := tmpfile.Write([]byte(fmt.Sprintf("error : %+v\n", iErr))); err != nil { + + if _, err := fmt.Fprintf(tmpfile, "error : %+v\n", iErr); err != nil { tmpfile.Close() log.Fatal(err) } - if _, err := tmpfile.Write([]byte(version.FullString())); err != nil { + + if _, err := tmpfile.WriteString(version.FullString()); err != nil { tmpfile.Close() log.Fatal(err) } + if _, err := tmpfile.Write(debug.Stack()); err != nil { tmpfile.Close() log.Fatal(err) } + if err := tmpfile.Close(); err != nil { log.Fatal(err) } + return tmpfile.Name() } @@ -38,6 +43,7 @@ func CatchPanic(component string) { if r := recover(); r != nil { log.Errorf("crowdsec - goroutine %s crashed : %s", component, r) log.Errorf("please report this error to https://github.com/crowdsecurity/crowdsec/") + filename := WriteStackTrace(r) log.Errorf("stacktrace/report is written to %s : please join it to your issue", filename) log.Fatalf("crowdsec stopped") diff --git a/version/version.go b/version/version.go index af349a2..8abc705 100644 --- a/version/version.go +++ b/version/version.go @@ -14,10 +14,10 @@ var ( ) func FullString() string { - ret := "" - ret += fmt.Sprintf("version: %s-%s\n", Version, Tag) + ret := fmt.Sprintf("version: %s-%s\n", Version, Tag) ret += fmt.Sprintf("BuildDate: %s\n", BuildDate) ret += fmt.Sprintf("GoVersion: %s\n", GoVersion) + return ret } diff --git a/yamlpatch/patcher.go b/yamlpatch/patcher.go index 071cd1c..8faddef 100644 --- a/yamlpatch/patcher.go +++ b/yamlpatch/patcher.go @@ -34,15 +34,13 @@ func (p *Patcher) SetQuiet(quiet bool) { // 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) { - var content []byte - - var err error - - if content, err = os.ReadFile(filePath); err != nil { + content, err := os.ReadFile(filePath) + if err != nil { return nil, fmt.Errorf("while reading yaml file: %w", err) } var yamlMap map[interface{}]interface{} + if err = yaml.Unmarshal(content, &yamlMap); err != nil { return nil, fmt.Errorf("%s: %w", filePath, err) } @@ -53,18 +51,12 @@ func readYAML(filePath string) ([]byte, error) { // MergedPatchContent reads a YAML file and, if it exists, its patch file, // then merges them and returns it serialized. func (p *Patcher) MergedPatchContent() ([]byte, error) { - var err error - - var base []byte - - base, err = readYAML(p.BaseFilePath) + base, err := readYAML(p.BaseFilePath) if err != nil { return nil, err } - var over []byte - - over, err = readYAML(p.PatchFilePath) + over, err := readYAML(p.PatchFilePath) if errors.Is(err, os.ErrNotExist) { return base, nil } @@ -77,13 +69,12 @@ func (p *Patcher) MergedPatchContent() ([]byte, error) { if p.quiet { logf = log.Debugf } - logf("Loading yaml file: '%s' with additional values from '%s'", p.BaseFilePath, p.PatchFilePath) - var patched *bytes.Buffer + logf("Loading yaml file: '%s' with additional values from '%s'", p.BaseFilePath, p.PatchFilePath) // strict mode true, will raise errors for duplicate map keys and // overriding with a different type - patched, err = YAML([][]byte{base, over}, true) + patched, err := YAML([][]byte{base, over}, true) if err != nil { return nil, err } @@ -94,11 +85,6 @@ func (p *Patcher) MergedPatchContent() ([]byte, error) { // read multiple YAML documents inside a file, and writes them to a buffer // separated by the appropriate '---' terminators. func decodeDocuments(file *os.File, buf *bytes.Buffer, finalDashes bool) error { - var ( - err error - docBytes []byte - ) - dec := yaml.NewDecoder(file) dec.SetStrict(true) @@ -107,29 +93,33 @@ func decodeDocuments(file *os.File, buf *bytes.Buffer, finalDashes bool) error { for { yml := make(map[interface{}]interface{}) - err = dec.Decode(&yml) + err := dec.Decode(&yml) if err != nil { if errors.Is(err, io.EOF) { break } + return fmt.Errorf("while decoding %s: %w", file.Name(), err) } - docBytes, err = yaml.Marshal(&yml) + docBytes, err := yaml.Marshal(&yml) if err != nil { return fmt.Errorf("while marshaling %s: %w", file.Name(), err) } if dashTerminator { - buf.Write([]byte("---\n")) + buf.WriteString("---\n") } buf.Write(docBytes) + dashTerminator = true } + if dashTerminator && finalDashes { - buf.Write([]byte("---\n")) + buf.WriteString("---\n") } + return nil } @@ -137,31 +127,29 @@ func decodeDocuments(file *os.File, buf *bytes.Buffer, finalDashes bool) error { // the content of the patch BEFORE the base document. The result is a multi-document // YAML in all cases, even if the base and patch files are single documents. func (p *Patcher) PrependedPatchContent() ([]byte, error) { - var ( - result bytes.Buffer - patchFile *os.File - baseFile *os.File - err error - ) - - patchFile, err = os.Open(p.PatchFilePath) + patchFile, err := os.Open(p.PatchFilePath) // optional file, ignore if it does not exist if err != nil && !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("while opening %s: %s", p.PatchFilePath, err) } + var result bytes.Buffer + if patchFile != nil { if err = decodeDocuments(patchFile, &result, true); err != nil { return nil, err } + logf := log.Infof + if p.quiet { logf = log.Debugf } + logf("Prepending yaml: '%s' with '%s'", p.BaseFilePath, p.PatchFilePath) } - baseFile, err = os.Open(p.BaseFilePath) + baseFile, err := os.Open(p.BaseFilePath) if err != nil { return nil, fmt.Errorf("while opening %s: %w", p.BaseFilePath, err) }