From 0d6a555e4145e947b9a4f0e98ee5df71bc79a404 Mon Sep 17 00:00:00 2001 From: DavidDiasN Date: Mon, 6 May 2024 23:30:39 -0400 Subject: [PATCH] Port lint-no-trailing-newline-in-log-messages sh -> Go --- .github/workflows/lint.reusable.yml | 2 +- ...int-no-trailing-newline-in-log-messages.sh | 32 ----------- ...int_no_trailing_newline_in_log_messages.go | 55 +++++++++++++++++++ 3 files changed, 56 insertions(+), 33 deletions(-) delete mode 100755 scripts/lint-no-trailing-newline-in-log-messages.sh create mode 100644 scripts/lint_no_trailing_newline_in_log_messages.go diff --git a/.github/workflows/lint.reusable.yml b/.github/workflows/lint.reusable.yml index 4af5775..213c3f6 100644 --- a/.github/workflows/lint.reusable.yml +++ b/.github/workflows/lint.reusable.yml @@ -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 diff --git a/scripts/lint-no-trailing-newline-in-log-messages.sh b/scripts/lint-no-trailing-newline-in-log-messages.sh deleted file mode 100755 index c3dd509..0000000 --- a/scripts/lint-no-trailing-newline-in-log-messages.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash -# SPDX-FileCopyrightText: 2023 The Pion community -# SPDX-License-Identifier: MIT - -set -e - -SCRIPT_PATH=$( - cd "$(dirname "${BASH_SOURCE[0]}")" - pwd -P -) -if [ -f ${SCRIPT_PATH}/.ci.conf ]; then - . ${SCRIPT_PATH}/.ci.conf -fi - -FILES=$( - find "${SCRIPT_PATH}/.." -name "*.go" \ - | while read FILE; do - EXCLUDED=false - for EXCLUDE_DIRECTORY in ${EXCLUDE_DIRECTORIES}; do - if [[ $file == */${EXCLUDE_DIRECTORY}/* ]]; then - EXCLUDED=true - break - fi - done - ${EXCLUDED} || echo "${FILE}" - done -) - -if grep -E '\.(Trace|Debug|Info|Warn|Error)f?\("[^"]*\\n"\)?' ${FILES} | grep -v -e 'nolint'; then - echo "Log format strings should have trailing new-line" - exit 1 -fi diff --git a/scripts/lint_no_trailing_newline_in_log_messages.go b/scripts/lint_no_trailing_newline_in_log_messages.go new file mode 100644 index 0000000..a63778f --- /dev/null +++ b/scripts/lint_no_trailing_newline_in_log_messages.go @@ -0,0 +1,55 @@ +/* +SPDX-FileCopyrightText: 2023 The Pion community +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 { + return err + } + + if info.IsDir() || filepath.Ext(path) != ".go" { + return nil + } + + fileContents, err := os.ReadFile(path) + if err != nil { + return err + } + + for _, match := range logReg.FindAll(fileContents, -1) { + if !strings.Contains(string(match), "nolint") { + fmt.Printf("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) + } +}