From cac5f610e757f27e2f9ff98d1c5decd415d6341f Mon Sep 17 00:00:00 2001 From: Ibrahim AshShohail Date: Wed, 9 Aug 2017 20:38:28 +0300 Subject: [PATCH] internal/gps: add isPreservedNonGoFile Signed-off-by: Ibrahim AshShohail --- internal/gps/prune.go | 51 +++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/internal/gps/prune.go b/internal/gps/prune.go index 455c5e7265..f2cd7399f2 100644 --- a/internal/gps/prune.go +++ b/internal/gps/prune.go @@ -32,9 +32,24 @@ const ( ) var ( - preservedNonGoFiles = []string{ - "LICENSE", - "COPYING", + // licenseFilePrefixes is a list of name prefixes for licesnse files. + licenseFilePrefixes = []string{ + "license", + "licence", + "copying", + "unlicense", + "copyright", + "copyleft", + } + // legalFileSubstrings contains substrings that are likey part of a legal + // declaration file. + legalFileSubstrings = []string{ + "legal", + "notice", + "disclaimer", + "patent", + "third-party", + "thirdparty", } ) @@ -215,22 +230,36 @@ func calculateNonGoFiles(baseDir string) ([]string, error) { return nil } - // Ignore preserved non-Go files. We check for prefix incase the file - // has an extension. For example: LICENSE.md. - for _, prefix := range preservedNonGoFiles { - if strings.HasPrefix(info.Name(), prefix) { - return nil - } + if !isPreservedNonGoFile(info.Name()) { + files = append(files, path) } - files = append(files, path) - return nil }) return files, err } +// isPreservedNonGoFile checks if the file name idicates that the file should be +// preserved. It assumes the file is not a Go file (doesn't have a .go suffix). +func isPreservedNonGoFile(name string) bool { + name = strings.ToLower(name) + + for _, prefix := range licenseFilePrefixes { + if strings.HasPrefix(name, prefix) { + return true + } + } + + for _, substring := range legalFileSubstrings { + if strings.Contains(name, substring) { + return true + } + } + + return false +} + // pruneGoTestFiles deletes all Go test files (*_test.go) within baseDirr. func pruneGoTestFiles(baseDir string, logger *log.Logger) error { files, err := calculateGoTestFiles(baseDir)