Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrote shell script as a go script #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 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,138 @@
package main

import (
"fmt"
"io/fs"
"os"
"regexp"
"strings"
)

var exDirs []string

func main() {

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

configFile := parentDir + "/.ci.conf"
exDirs, err = parseBashStringArray("EXCLUDE_DIRECTORIES", configFile)
if err != nil {
fmt.Println(err)
return
}

var filesToCheck []string
err = fs.WalkDir(os.DirFS(parentDir+"/.."), ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if isExcluded(path) {
return fs.SkipDir
}
if d.IsDir() || !strings.HasSuffix(d.Name(), ".go") {
return nil
}

filesToCheck = append(filesToCheck, path)
return nil
})

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

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

for _, filename := range filesToCheck {
file, err := os.Open(parentDir + "/../" + filename)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()

info, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}

buff := make([]byte, info.Size())

_, err = file.Read(buff)
if err != nil {
fmt.Println(err)
return
}

foundArr := logReg.FindAll(buff, -1)
for _, match := range foundArr {
if !strings.Contains(string(match), "nolint") {
fmt.Println("Log format strings should have trailing new-line")
os.Exit(1)
}
}
}
}

func isExcluded(name string) bool {
for _, dir := range exDirs {
if name == dir {
return true
}
}
return false
}

func parseBashStringArray(variable, filename string) (arr []string, err error) {

file, err := os.Open(filename)
if err != nil {
return []string{}, err
}

defer file.Close()

info, err := file.Stat()
if err != nil {
return []string{}, err
}

buff := make([]byte, info.Size())

_, err = file.Read(buff)
if err != nil {
return []string{}, err
}

fileString := string(buff)

lines := strings.Split(fileString, "\n")
bashLine := ""
for line := range lines {
if strings.Contains(lines[line], variable) {
bashLine = lines[line]
break
}
}

if bashLine == "" {
return []string{}, fmt.Errorf("%s not found error", variable)
}

bashLine = strings.TrimPrefix(bashLine, variable+"=")
bashArray := strings.Fields(bashLine)
bashArray[0] = bashArray[0][1:]
bashArray[len(bashArray)-1] = strings.TrimRight(bashArray[len(bashArray)-1], ")")
return bashArray, nil
}