-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (131 loc) · 3.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"regexp"
"strings"
)
// Change this Path to directory where you want to run this script
const directoryPath = "./testCase1"
// regex pattern to match log functions (e.g., log.Error(), log.Info() etc.)
var logFuncPattern = regexp.MustCompile(`^log\.[A-Za-z]+$`)
// regex pattern to match log functions (e.g., fmt.Print(), fmt.Println() etc.)
var fmtFuncPattern = regexp.MustCompile(`^fmt\.[A-Za-z]+$`)
// checkLogStatements checks if all log statements in the function body follow the convention.
func checkLogStatements(funcName string, block *ast.BlockStmt) bool {
hasLogStatement := false
allFollowConvention := true
for _, stmt := range block.List {
if containsLogCall(stmt) {
hasLogStatement = true
if !followingConvention(funcName, stmt) {
allFollowConvention = false
}
}
}
if hasLogStatement && !allFollowConvention {
fmt.Print(" \033[33m") // For Yellow colors
fmt.Printf("Function: %s has some log statements not following the convention", funcName)
fmt.Print("\033[0m\n") // For colors
return false
}
if hasLogStatement {
fmt.Print(" \033[32m") // For Green color
fmt.Printf("Function: %s is following convention", funcName)
fmt.Print("\033[0m\n") // For colors
}
return true
}
func followingConvention(funcName string, stmt ast.Stmt) bool {
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
return false
}
callExpr, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
return false
}
if len(callExpr.Args) == 0 {
return false
}
basicLit, ok := callExpr.Args[0].(*ast.BasicLit)
if !ok || basicLit.Kind != token.STRING {
return false
}
logMessage := strings.Trim(basicLit.Value, `"`)
// fmt.Println("logMessage:", logMessage)
return strings.HasPrefix(logMessage, funcName)
}
func containsLogCall(stmt ast.Stmt) bool {
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
return false
}
callExpr, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
return false
}
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
return false
}
// Combine the package name and function name to match with the regex
fullFuncName := fmt.Sprintf("%s.%s", selExpr.X, selExpr.Sel.Name)
// fmt.Println("fullFuncName", fullFuncName)
return (logFuncPattern.MatchString(fullFuncName) || fmtFuncPattern.MatchString(fullFuncName))
}
func inspectNode(n ast.Node, hasMissingLog *bool) bool {
fn, ok := n.(*ast.FuncDecl)
if !ok || fn.Body == nil {
return true
}
funcName := fn.Name.Name
fmt.Println(" Checking Function: ",funcName,"()")
if !checkLogStatements(funcName, fn.Body) {
*hasMissingLog = true
}
return true
}
// processFile processes a single Go file to check for log statement conventions.
func processFile(filePath string, hasMissingLog *bool) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filePath, nil, parser.AllErrors)
if err != nil {
fmt.Printf("Failed to parse file %s: %v\n", filePath, err)
return
}
// Inspect the AST and check each function declaration.
ast.Inspect(node, func(n ast.Node) bool {
return inspectNode(n, hasMissingLog)
})
}
// processDirectory recursively processes all .go files in a directory.
func processDirectory(dirPath string, hasMissingLog *bool) {
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") {
fmt.Println("Processing file: ", path)
processFile(path, hasMissingLog)
}
return nil
})
if err != nil {
fmt.Printf("Failed to process directory %s: %v\n", dirPath, err)
}
}
func main() {
dirPath := directoryPath // change this on top
hasMissingLog := false
processDirectory(dirPath, &hasMissingLog)
if !hasMissingLog {
fmt.Print("\033[32m") // For Green color
fmt.Println("All functions have log statements following the convention.")
fmt.Print("\033[0m\n") // For colors
}
}