Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit cac5f61

Browse files
committed
internal/gps: add isPreservedNonGoFile
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent c417150 commit cac5f61

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

internal/gps/prune.go

+40-11
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,24 @@ const (
3232
)
3333

3434
var (
35-
preservedNonGoFiles = []string{
36-
"LICENSE",
37-
"COPYING",
35+
// licenseFilePrefixes is a list of name prefixes for licesnse files.
36+
licenseFilePrefixes = []string{
37+
"license",
38+
"licence",
39+
"copying",
40+
"unlicense",
41+
"copyright",
42+
"copyleft",
43+
}
44+
// legalFileSubstrings contains substrings that are likey part of a legal
45+
// declaration file.
46+
legalFileSubstrings = []string{
47+
"legal",
48+
"notice",
49+
"disclaimer",
50+
"patent",
51+
"third-party",
52+
"thirdparty",
3853
}
3954
)
4055

@@ -215,22 +230,36 @@ func calculateNonGoFiles(baseDir string) ([]string, error) {
215230
return nil
216231
}
217232

218-
// Ignore preserved non-Go files. We check for prefix incase the file
219-
// has an extension. For example: LICENSE.md.
220-
for _, prefix := range preservedNonGoFiles {
221-
if strings.HasPrefix(info.Name(), prefix) {
222-
return nil
223-
}
233+
if !isPreservedNonGoFile(info.Name()) {
234+
files = append(files, path)
224235
}
225236

226-
files = append(files, path)
227-
228237
return nil
229238
})
230239

231240
return files, err
232241
}
233242

243+
// isPreservedNonGoFile checks if the file name idicates that the file should be
244+
// preserved. It assumes the file is not a Go file (doesn't have a .go suffix).
245+
func isPreservedNonGoFile(name string) bool {
246+
name = strings.ToLower(name)
247+
248+
for _, prefix := range licenseFilePrefixes {
249+
if strings.HasPrefix(name, prefix) {
250+
return true
251+
}
252+
}
253+
254+
for _, substring := range legalFileSubstrings {
255+
if strings.Contains(name, substring) {
256+
return true
257+
}
258+
}
259+
260+
return false
261+
}
262+
234263
// pruneGoTestFiles deletes all Go test files (*_test.go) within baseDirr.
235264
func pruneGoTestFiles(baseDir string, logger *log.Logger) error {
236265
files, err := calculateGoTestFiles(baseDir)

0 commit comments

Comments
 (0)