Skip to content

Commit

Permalink
Port lint-no-trailing-newline-in-log-messages
Browse files Browse the repository at this point in the history
sh -> Go
  • Loading branch information
DavidDiasN authored and Sean-Der committed Nov 5, 2024
1 parent 51bda7c commit c6c47a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
run: go run .github/.goassets/scripts/lint_filename.go

- name: Logging messages should not have trailing newlines
run: .github/.goassets/scripts/lint-no-trailing-newline-in-log-messages.sh
run: go run .github/.goassets/scripts/lint_no_trailing_newline_in_log_messages.go

- name: Go version in go.mod
run: .github/.goassets/scripts/lint-go-mod-version.sh
Expand Down
32 changes: 0 additions & 32 deletions scripts/lint-no-trailing-newline-in-log-messages.sh

This file was deleted.

51 changes: 51 additions & 0 deletions scripts/lint_no_trailing_newline_in_log_messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
SPDX-License-Identifier: MIT
*/
package main

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

func main() {
parentDir, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

logReg, err := regexp.Compile(`\.(Trace|Debug|Info|Warn|Error)f?\("[^"]*\\n"\)?`)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

err = filepath.Walk(parentDir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || filepath.Ext(path) != ".go" {
return err
}

fileContents, err := os.ReadFile(path)
if err != nil {
return err
}

for _, match := range logReg.FindAll(fileContents, -1) {
if !strings.Contains(string(match), "nolint") {
return fmt.Errorf("Log format strings should have trailing new-line: %s\n", match)
os.Exit(1)
}
}

return nil
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

0 comments on commit c6c47a7

Please sign in to comment.