Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 26, 2024
1 parent 5be89e7 commit 373847a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 40 deletions.
4 changes: 4 additions & 0 deletions coalesce/coalesce.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func String(s ...string) string {
return v
}
}

return ""
}

Expand All @@ -17,6 +18,7 @@ func Int(s ...int) int {
return v
}
}

return 0
}

Expand All @@ -27,6 +29,7 @@ func NotNil[T any](args ...*T) *T {
return arg
}
}

return nil
}

Expand All @@ -40,6 +43,7 @@ func NotEmptyOrNil[T comparable](args ...*T) *T {
return arg
}
}
return nil
}
*/
2 changes: 2 additions & 0 deletions csdaemon/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions csstring/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions cstest/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions slicetools/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
10 changes: 8 additions & 2 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
56 changes: 22 additions & 34 deletions yamlpatch/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)

Expand All @@ -107,61 +93,63 @@ 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
}

// PrependedPatchContent collates the base .yaml file with the .yaml.patch, by putting
// 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)
}
Expand Down

0 comments on commit 373847a

Please sign in to comment.