1
1
package checker
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"path/filepath"
7
+ "regexp"
6
8
"strings"
7
9
8
10
"github.com/kubecub/standardizer/config"
@@ -24,7 +26,24 @@ type Checker struct {
24
26
}
25
27
26
28
func (c * Checker ) Check () error {
27
- return filepath .Walk (c .Config .BaseConfig .SearchDirectory , c .checkPath )
29
+ err := filepath .Walk (c .Config .BaseConfig .SearchDirectory , c .checkPath )
30
+ if err != nil {
31
+ return err
32
+ }
33
+
34
+ if len (c .Summary .Issues ) > 0 {
35
+ c .printIssues ()
36
+ return fmt .Errorf ("found %d issues in the codebase" , len (c .Summary .Issues ))
37
+ }
38
+ return nil
39
+ }
40
+
41
+ func (c * Checker ) printIssues () {
42
+ fmt .Println ("## Issues found:" )
43
+ fmt .Println ("===================================================================================================" )
44
+ for _ , issue := range c .Summary .Issues {
45
+ fmt .Printf ("Type: %s, Path: %s, Message: %s\n " , issue .Type , issue .Path , issue .Message )
46
+ }
28
47
}
29
48
30
49
func (c * Checker ) checkPath (path string , info os.FileInfo , err error ) error {
@@ -42,15 +61,10 @@ func (c *Checker) checkPath(path string, info os.FileInfo, err error) error {
42
61
}
43
62
44
63
if info .IsDir () {
45
- c .Summary .CheckedDirectories ++
46
64
if c .isIgnoredDirectory (relativePath ) {
47
- c .Summary .Issues = append (c .Summary .Issues , Issue {
48
- Type : "ignoredDirectory" ,
49
- Path : path ,
50
- Message : "This directory has been ignored" ,
51
- })
52
65
return filepath .SkipDir
53
66
}
67
+ c .Summary .CheckedDirectories ++
54
68
if ! c .checkDirectoryName (relativePath ) {
55
69
c .Summary .Issues = append (c .Summary .Issues , Issue {
56
70
Type : "directoryNaming" ,
@@ -77,17 +91,21 @@ func (c *Checker) checkPath(path string, info os.FileInfo, err error) error {
77
91
78
92
func (c * Checker ) isIgnoredDirectory (path string ) bool {
79
93
for _ , ignoredDir := range c .Config .IgnoreDirectories {
80
- if strings .Contains (path , ignoredDir ) {
94
+ if strings .HasSuffix ( path , ignoredDir ) || strings . Contains (path , ignoredDir + "/" ) {
81
95
return true
82
96
}
83
97
}
84
98
return false
85
99
}
86
100
87
101
func (c * Checker ) isIgnoredFile (path string ) bool {
88
- ext := filepath .Ext (path )
89
102
for _ , format := range c .Config .IgnoreFormats {
90
- if ext == format {
103
+ matched , err := regexp .MatchString (format , path )
104
+ if err != nil {
105
+ fmt .Printf ("Invalid regex pattern: %s, error: %s\n " , format , err )
106
+ continue
107
+ }
108
+ if matched {
91
109
return true
92
110
}
93
111
}
@@ -96,21 +114,34 @@ func (c *Checker) isIgnoredFile(path string) bool {
96
114
97
115
func (c * Checker ) checkDirectoryName (path string ) bool {
98
116
dirName := filepath .Base (path )
117
+ errors := []string {}
118
+
99
119
if c .Config .DirectoryNaming .MustBeLowercase && (dirName != strings .ToLower (dirName )) {
100
- return false
120
+ errors = append ( errors , "directory name must be lowercase" )
101
121
}
102
122
if ! c .Config .DirectoryNaming .AllowHyphens && strings .Contains (dirName , "-" ) {
103
- return false
123
+ errors = append ( errors , "hyphens are not allowed in directory names" )
104
124
}
105
125
if ! c .Config .DirectoryNaming .AllowUnderscores && strings .Contains (dirName , "_" ) {
126
+ errors = append (errors , "underscores are not allowed in directory names" )
127
+ }
128
+
129
+ if len (errors ) > 0 {
130
+ c .Summary .Issues = append (c .Summary .Issues , Issue {
131
+ Type : "directoryNaming" ,
132
+ Path : path ,
133
+ Message : fmt .Sprintf ("Directory naming issues: %s. Example of valid directory name: '%s'" , strings .Join (errors , "; " ), c .exampleDirectoryName ()),
134
+ })
106
135
return false
107
136
}
137
+
108
138
return true
109
139
}
110
140
111
141
func (c * Checker ) checkFileName (path string ) bool {
112
142
fileName := filepath .Base (path )
113
143
ext := filepath .Ext (fileName )
144
+ errors := []string {}
114
145
115
146
allowHyphens := c .Config .FileNaming .AllowHyphens
116
147
allowUnderscores := c .Config .FileNaming .AllowUnderscores
@@ -123,14 +154,69 @@ func (c *Checker) checkFileName(path string) bool {
123
154
}
124
155
125
156
if mustBeLowercase && (fileName != strings .ToLower (fileName )) {
126
- return false
157
+ errors = append ( errors , "file name must be lowercase" )
127
158
}
128
159
if ! allowHyphens && strings .Contains (fileName , "-" ) {
129
- return false
160
+ errors = append ( errors , "hyphens are not allowed in file names" )
130
161
}
131
162
if ! allowUnderscores && strings .Contains (fileName , "_" ) {
163
+ errors = append (errors , "underscores are not allowed in file names" )
164
+ }
165
+
166
+ if len (errors ) > 0 {
167
+ c .Summary .Issues = append (c .Summary .Issues , Issue {
168
+ Type : "fileNaming" ,
169
+ Path : path ,
170
+ Message : fmt .Sprintf ("File naming issues: %s. Example of valid file name: '%s'" , strings .Join (errors , "; " ), c .exampleFileName (ext )),
171
+ })
132
172
return false
133
173
}
134
174
135
175
return true
136
176
}
177
+
178
+ func (c * Checker ) exampleDirectoryName () string {
179
+ exampleName := "ExampleDirectory"
180
+ if c .Config .DirectoryNaming .MustBeLowercase {
181
+ exampleName = strings .ToLower (exampleName )
182
+ }
183
+ if ! c .Config .DirectoryNaming .AllowHyphens && ! c .Config .DirectoryNaming .AllowUnderscores {
184
+ exampleName = strings .ReplaceAll (exampleName , "-" , "" )
185
+ exampleName = strings .ReplaceAll (exampleName , "_" , "" )
186
+ } else if c .Config .DirectoryNaming .AllowHyphens {
187
+ exampleName = strings .ReplaceAll (exampleName , "_" , "-" )
188
+ } else if c .Config .DirectoryNaming .AllowUnderscores {
189
+ exampleName = strings .ReplaceAll (exampleName , "-" , "_" )
190
+ }
191
+ return exampleName
192
+ }
193
+
194
+ func (c * Checker ) exampleFileName (ext string ) string {
195
+ baseName := "example_file"
196
+ if c .Config .FileNaming .MustBeLowercase {
197
+ baseName = strings .ToLower (baseName )
198
+ }
199
+
200
+ if ! c .Config .FileNaming .AllowHyphens && ! c .Config .FileNaming .AllowUnderscores {
201
+ baseName = strings .ReplaceAll (baseName , "-" , "" )
202
+ baseName = strings .ReplaceAll (baseName , "_" , "" )
203
+ } else if c .Config .FileNaming .AllowHyphens {
204
+ } else if c .Config .FileNaming .AllowUnderscores {
205
+ baseName = strings .ReplaceAll (baseName , "-" , "_" )
206
+ }
207
+
208
+ if specificNaming , ok := c .Config .FileTypeSpecificNaming [ext ]; ok {
209
+ if specificNaming .MustBeLowercase {
210
+ baseName = strings .ToLower (baseName )
211
+ }
212
+ if ! specificNaming .AllowHyphens && ! specificNaming .AllowUnderscores {
213
+ baseName = strings .ReplaceAll (baseName , "-" , "" )
214
+ baseName = strings .ReplaceAll (baseName , "_" , "" )
215
+ } else if specificNaming .AllowHyphens {
216
+ } else if specificNaming .AllowUnderscores {
217
+ baseName = strings .ReplaceAll (baseName , "-" , "_" )
218
+ }
219
+ }
220
+
221
+ return fmt .Sprintf ("%s%s" , baseName , ext )
222
+ }
0 commit comments