From b06cd2f582e8db25942e41ec7693e69a0e0e76c0 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Sat, 29 May 2021 13:11:18 +0800 Subject: [PATCH] Expose license-location-threshold as config item --- .licenserc.yaml | 4 ++++ pkg/header/check.go | 14 ++++---------- pkg/header/config.go | 8 ++++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/.licenserc.yaml b/.licenserc.yaml index fbf15b1..af8bc1c 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -76,6 +76,10 @@ header: # `header` section is configurations for source codes license header. comment: on-failure # on what condition license-eye will comment on the pull request, `on-failure`, `always`, `never`. + # license-location-threshold specifies the index threshold where the license header can be located, + # after all, a "header" cannot be TOO far from the file start. + license-location-threshold: 80 + dependency: files: - go.mod diff --git a/pkg/header/check.go b/pkg/header/check.go index b7b87c1..21bdb8d 100644 --- a/pkg/header/check.go +++ b/pkg/header/check.go @@ -31,12 +31,6 @@ import ( "github.com/bmatcuk/doublestar/v2" ) -var ( - // LicenseLocationThreshold specifies the index threshold where the license header can be located, - // after all, a "header" cannot be TOO far from the file start. - LicenseLocationThreshold = 80 -) - // Check checks the license headers of the specified paths/globs. func Check(config *ConfigHeader, result *Result) error { for _, pattern := range config.Paths { @@ -129,7 +123,7 @@ func CheckFile(file string, config *ConfigHeader, result *Result) error { content := lcs.NormalizeHeader(string(bs)) expected, pattern := config.NormalizedLicense(), config.NormalizedPattern() - if satisfy(content, expected, pattern) { + if satisfy(content, config, expected, pattern) { result.Succeed(file) } else { logger.Log.Debugln("Content is:", content) @@ -140,9 +134,9 @@ func CheckFile(file string, config *ConfigHeader, result *Result) error { return nil } -func satisfy(content, license string, pattern *regexp.Regexp) bool { +func satisfy(content string, config *ConfigHeader, license string, pattern *regexp.Regexp) bool { if index := strings.Index(content, license); strings.TrimSpace(license) != "" && index >= 0 { - return index < LicenseLocationThreshold + return index < config.LicenseLocationThreshold } if pattern == nil { @@ -150,5 +144,5 @@ func satisfy(content, license string, pattern *regexp.Regexp) bool { } index := pattern.FindStringIndex(content) - return len(index) == 2 && index[0] < LicenseLocationThreshold + return len(index) == 2 && index[0] < config.LicenseLocationThreshold } diff --git a/pkg/header/config.go b/pkg/header/config.go index b4f0700..6abb94a 100644 --- a/pkg/header/config.go +++ b/pkg/header/config.go @@ -56,6 +56,10 @@ type ConfigHeader struct { Paths []string `yaml:"paths"` PathsIgnore []string `yaml:"paths-ignore"` Comment CommentOption `yaml:"comment"` + + // LicenseLocationThreshold specifies the index threshold where the license header can be located, + // after all, a "header" cannot be TOO far from the file start. + LicenseLocationThreshold int `yaml:"license-location-threshold"` } // NormalizedLicense returns the normalized string of the license content, @@ -120,6 +124,10 @@ func (config *ConfigHeader) Finalize() error { logger.Log.Debugln("Pattern is:", p) } + if config.LicenseLocationThreshold <= 0 { + config.LicenseLocationThreshold = 80 + } + return nil }