@@ -29,6 +29,43 @@ import (
29
29
"strings"
30
30
)
31
31
32
+ const (
33
+ goLicenseFile = "/app/LICENSE-HEADER-GO.txt" // Change this to the path of your license file
34
+ genericLicenseHeaderFile = "/app/LICENSE-HEADER-ALL.txt"
35
+ shellLicenseHeaderFile = "/app/LICENSE-HEADER-SHELL.txt"
36
+ rootDir = "." // Change this to the directory you want to search
37
+ shellExtensions = ".sh"
38
+ yamlExtensions = ".yaml"
39
+ dockerExtensions = "Dockerfile"
40
+ goExtensions = ".go"
41
+ )
42
+
43
+ func main () {
44
+ isAutofixEnabled := flag .Bool ("auto-fix" , false , "Autofix enabled" )
45
+ flag .Parse ()
46
+
47
+ hasLicense , err := checkGoLicenseHeader (isAutofixEnabled )
48
+ if err != nil {
49
+ fmt .Println ("Error checking go license header:" , err )
50
+ }
51
+ hasLicense , err = checkShellLicenseHeader (isAutofixEnabled )
52
+ if err != nil {
53
+ fmt .Println ("Error checking shell license header:" , err )
54
+ }
55
+ hasLicense , err = checkYamlLicenseHeader (isAutofixEnabled )
56
+ if err != nil {
57
+ fmt .Println ("Error checking YAML license header:" , err )
58
+ }
59
+ hasLicense , err = checkDockerFileLicenseHeader (isAutofixEnabled )
60
+ if err != nil {
61
+ fmt .Println ("Error checking Dockerfile license header:" , err )
62
+ }
63
+ // if any of the license headers are missing or incorrect then exit with error
64
+ if ! hasLicense {
65
+ os .Exit (1 )
66
+ }
67
+ }
68
+
32
69
func readLicenseHeader (filePath string ) (string , error ) {
33
70
file , err := os .Open (filePath )
34
71
if err != nil {
@@ -41,7 +78,7 @@ func readLicenseHeader(filePath string) (string, error) {
41
78
for scanner .Scan () {
42
79
headerLines = append (headerLines , scanner .Text ())
43
80
}
44
- if err : = scanner .Err (); err != nil {
81
+ if err = scanner .Err (); err != nil {
45
82
return "" , err
46
83
}
47
84
return strings .Join (headerLines , "\n " ), nil
@@ -72,22 +109,6 @@ func checkLicenseHeader(filePath, licenseHeader string) (bool, error) {
72
109
return true , scanner .Err ()
73
110
}
74
111
75
- func listGoFiles (root string ) ([]string , error ) {
76
- var files []string
77
- err := filepath .WalkDir (root , func (path string , dirEntry fs.DirEntry , err error ) error {
78
- if err != nil {
79
- return err
80
- }
81
- if ! dirEntry .IsDir () &&
82
- strings .HasSuffix (dirEntry .Name (), ".go" ) &&
83
- ! strings .Contains (dirEntry .Name (), "mock" ) &&
84
- ! strings .Contains (dirEntry .Name (), "generated" ) {
85
- files = append (files , path )
86
- }
87
- return nil
88
- })
89
- return files , err
90
- }
91
112
func autofixLicenseHeader (filePath , licenseHeader string ) error {
92
113
input , err := os .ReadFile (filePath )
93
114
if err != nil {
@@ -98,25 +119,91 @@ func autofixLicenseHeader(filePath, licenseHeader string) error {
98
119
return os .WriteFile (filePath , []byte (output ), 0644 )
99
120
}
100
121
101
- func main () {
102
- isAutofixEnabled := flag .Bool ("auto-fix" , false , "Autofix enabled" )
103
- flag .Parse ()
104
-
105
- licenseFile := "/app/LICENSE-HEADER.txt" // Change this to the path of your license file
122
+ func checkGoLicenseHeader (isAutofixEnabled * bool ) (bool , error ) {
123
+ licenseHeader , err := readLicenseHeader (goLicenseFile )
124
+ if err != nil {
125
+ fmt .Println ("Error reading license file:" , err )
126
+ return false , err
127
+ }
128
+ files , err := listFilesByExtension (goExtensions )
129
+ if err != nil {
130
+ return false , err
131
+ }
132
+ fmt .Println ("Checking license header for the following go files:" )
133
+ for _ , file := range files {
134
+ fmt .Println (file )
135
+ }
136
+ var hasLicense bool
137
+ for _ , file := range files {
138
+ hasLicense , err = checkLicenseHeader (file , licenseHeader )
139
+ if err != nil {
140
+ fmt .Printf ("Error checking file %s: %v\n " , file , err )
141
+ continue
142
+ }
143
+ if ! hasLicense {
144
+ fmt .Printf ("Missing or incorrect license header: %s\n " , file )
145
+ // if auto-fix is enabled then only we will fix the license headers else just report valid header and exit
146
+ if * isAutofixEnabled {
147
+ err = autofixLicenseHeader (file , licenseHeader )
148
+ if err != nil {
149
+ fmt .Printf ("Error updating license header for file %s: %v\n " , file , err )
150
+ } else {
151
+ fmt .Printf ("License header updated for file: %s\n " , file )
152
+ }
153
+ }
154
+ }
155
+ }
156
+ return hasLicense , nil
157
+ }
106
158
107
- licenseHeader , err := readLicenseHeader (licenseFile )
159
+ func checkShellLicenseHeader (isAutofixEnabled * bool ) (bool , error ) {
160
+ licenseHeader , err := readLicenseHeader (shellLicenseHeaderFile )
108
161
if err != nil {
109
162
fmt .Println ("Error reading license file:" , err )
110
- return
163
+ return false , err
164
+ }
165
+ files , err := listFilesByExtension (shellExtensions )
166
+ if err != nil {
167
+ return false , err
111
168
}
169
+ fmt .Println ("Checking license header for the following shell script files:" )
170
+ for _ , file := range files {
171
+ fmt .Println (file )
172
+ }
173
+ var hasLicense bool
174
+ for _ , file := range files {
175
+ hasLicense , err = checkLicenseHeader (file , licenseHeader )
176
+ if err != nil {
177
+ fmt .Printf ("Error checking file %s: %v\n " , file , err )
178
+ continue
179
+ }
180
+ if ! hasLicense {
181
+ fmt .Printf ("Missing or incorrect license header: %s\n " , file )
182
+ // if auto-fix is enabled then only we will fix the license headers else just report valid header and exit
183
+ if * isAutofixEnabled {
184
+ err = autofixLicenseHeader (file , licenseHeader )
185
+ if err != nil {
186
+ fmt .Printf ("Error updating license header for file %s: %v\n " , file , err )
187
+ } else {
188
+ fmt .Printf ("License header updated for file: %s\n " , file )
189
+ }
190
+ }
191
+ }
192
+ }
193
+ return hasLicense , nil
194
+ }
112
195
113
- root := "." // Change this to the directory you want to search
114
- files , err := listGoFiles ( root )
196
+ func checkDockerFileLicenseHeader ( isAutofixEnabled * bool ) ( bool , error ) {
197
+ licenseHeader , err := readLicenseHeader ( genericLicenseHeaderFile )
115
198
if err != nil {
116
- fmt .Println ("Error:" , err )
117
- return
199
+ fmt .Println ("Error reading license file :" , err )
200
+ return false , err
118
201
}
119
- fmt .Println ("Checking license header for the following files:" )
202
+ files , err := listFilesByExtension (dockerExtensions )
203
+ if err != nil {
204
+ return false , err
205
+ }
206
+ fmt .Println ("Checking license header for the following Docker files:" )
120
207
for _ , file := range files {
121
208
fmt .Println (file )
122
209
}
@@ -131,7 +218,7 @@ func main() {
131
218
fmt .Printf ("Missing or incorrect license header: %s\n " , file )
132
219
// if auto-fix is enabled then only we will fix the license headers else just report valid header and exit
133
220
if * isAutofixEnabled {
134
- err : = autofixLicenseHeader (file , licenseHeader )
221
+ err = autofixLicenseHeader (file , licenseHeader )
135
222
if err != nil {
136
223
fmt .Printf ("Error updating license header for file %s: %v\n " , file , err )
137
224
} else {
@@ -140,7 +227,67 @@ func main() {
140
227
}
141
228
}
142
229
}
143
- if ! hasLicense {
144
- os .Exit (1 )
230
+ return hasLicense , nil
231
+ }
232
+
233
+ func checkYamlLicenseHeader (isAutofixEnabled * bool ) (bool , error ) {
234
+ licenseHeader , err := readLicenseHeader (genericLicenseHeaderFile )
235
+ if err != nil {
236
+ fmt .Println ("Error reading license file:" , err )
237
+ return false , err
145
238
}
239
+ files , err := listFilesByExtension (yamlExtensions )
240
+ if err != nil {
241
+ return false , err
242
+ }
243
+ fmt .Println ("Checking license header for the following YAML files:" )
244
+ for _ , file := range files {
245
+ fmt .Println (file )
246
+ }
247
+ var hasLicense bool
248
+ for _ , file := range files {
249
+ hasLicense , err = checkLicenseHeader (file , licenseHeader )
250
+ if err != nil {
251
+ fmt .Printf ("Error checking file %s: %v\n " , file , err )
252
+ continue
253
+ }
254
+ if ! hasLicense {
255
+ fmt .Printf ("Missing or incorrect license header: %s\n " , file )
256
+ // if auto-fix is enabled then only we will fix the license headers else just report valid header and exit
257
+ if * isAutofixEnabled {
258
+ err = autofixLicenseHeader (file , licenseHeader )
259
+ if err != nil {
260
+ fmt .Printf ("Error updating license header for file %s: %v\n " , file , err )
261
+ } else {
262
+ fmt .Printf ("License header updated for file: %s\n " , file )
263
+ }
264
+ }
265
+ }
266
+ }
267
+ return hasLicense , nil
268
+ }
269
+
270
+ func listFilesByExtension (extension string ) ([]string , error ) {
271
+ var files []string
272
+ err := filepath .WalkDir (rootDir , func (path string , dirEntry fs.DirEntry , err error ) error {
273
+ if err != nil {
274
+ return err
275
+ }
276
+ // we will check for generated and mock files for go extension only else search for other file types
277
+ if extension == goExtensions {
278
+ if ! dirEntry .IsDir () &&
279
+ strings .HasSuffix (dirEntry .Name (), extension ) &&
280
+ ! strings .Contains (dirEntry .Name (), "mock" ) &&
281
+ ! strings .Contains (dirEntry .Name (), "generated" ) {
282
+ files = append (files , path )
283
+ }
284
+ } else {
285
+ if ! dirEntry .IsDir () &&
286
+ strings .HasSuffix (dirEntry .Name (), extension ) {
287
+ files = append (files , path )
288
+ }
289
+ }
290
+ return nil
291
+ })
292
+ return files , err
146
293
}
0 commit comments