diff --git a/cmd/root.go b/cmd/root.go index 43bf50b..8f5cc24 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -174,7 +174,7 @@ func findLicensesInDirectory(cfg *viper.Viper) error { options := identifier.Options{ ForceResult: true, - Patterns: identifier.SUPPORTED_MATCH_PATTERNS, + Patterns: configurer.SUPPORTED_MATCH_PATTERNS, Enhancements: identifier.Enhancements{ AddNotes: "", AddTextBlocks: true, diff --git a/configurer/configurer.go b/configurer/configurer.go index deb8ac1..288d9e9 100644 --- a/configurer/configurer.go +++ b/configurer/configurer.go @@ -8,9 +8,9 @@ import ( "path" "path/filepath" "runtime" + "strings" "github.com/spf13/pflag" - "github.com/spf13/viper" ) @@ -38,6 +38,16 @@ const ( PatternsFlag = "patterns" ) +const ( + MATCH_PATTERN_SPDX_ID = "spdx-id" + MATCH_PATTERN_ALIAS = "alias" + MATCH_PATTERN_URL = "url" + MATCH_PATTERN_PRIMARY = "primary" + MATCH_PATTERN_ASSOCIATED = "associated" +) + +var SUPPORTED_MATCH_PATTERNS = []string{MATCH_PATTERN_SPDX_ID, MATCH_PATTERN_ALIAS, MATCH_PATTERN_URL, MATCH_PATTERN_PRIMARY, MATCH_PATTERN_ASSOCIATED} + var ( _, thisFile, _, _ = runtime.Caller(0) // Dirs/files are relative to this file thisDir = filepath.Dir(thisFile) @@ -148,5 +158,8 @@ func AddDefaultFlags(flagSet *pflag.FlagSet) { flagSet.String(SpdxPathFlag, "", "Path to external SPDX templates to use") flagSet.String(CustomFlag, DefaultResource, "Custom templates to use") flagSet.String(CustomPathFlag, "", "Path to external custom templates to use") - flagSet.StringP(PatternsFlag, "", "", "List of pattern matching functions to execute") + + help_msg_pattern := fmt.Sprintf("Comma-separated list of license pattern-matching functions to execute. One or more of: [%v]; defaults to all patterns.", + strings.Join(SUPPORTED_MATCH_PATTERNS, ", ")) + flagSet.StringP(PatternsFlag, "", "", help_msg_pattern) } diff --git a/identifier/identifier.go b/identifier/identifier.go index a3abf7d..4b5fe0a 100644 --- a/identifier/identifier.go +++ b/identifier/identifier.go @@ -17,6 +17,7 @@ import ( "golang.org/x/exp/slices" "golang.org/x/sync/errgroup" + "github.com/CycloneDX/license-scanner/configurer" "github.com/CycloneDX/license-scanner/licenses" "github.com/CycloneDX/license-scanner/normalizer" ) @@ -26,15 +27,15 @@ var ( nonAlphaRE = regexp.MustCompile(`^[^A-Za-z0-9]*$`) ) -const ( - MATCH_PATTERN_SPDX_ID = "spdx-id" - MATCH_PATTERN_ALIAS = "alias" - MATCH_PATTERN_URL = "url" - MATCH_PATTERN_PRIMARY = "primary" - MATCH_PATTERN_ASSOCIATED = "associated" -) +// const ( +// MATCH_PATTERN_SPDX_ID = "spdx-id" +// MATCH_PATTERN_ALIAS = "alias" +// MATCH_PATTERN_URL = "url" +// MATCH_PATTERN_PRIMARY = "primary" +// MATCH_PATTERN_ASSOCIATED = "associated" +// ) -var SUPPORTED_MATCH_PATTERNS = []string{MATCH_PATTERN_SPDX_ID, MATCH_PATTERN_ALIAS, MATCH_PATTERN_URL, MATCH_PATTERN_PRIMARY, MATCH_PATTERN_ASSOCIATED} +// var SUPPORTED_MATCH_PATTERNS = []string{MATCH_PATTERN_SPDX_ID, MATCH_PATTERN_ALIAS, MATCH_PATTERN_URL, MATCH_PATTERN_PRIMARY, MATCH_PATTERN_ASSOCIATED} type Options struct { ForceResult bool @@ -124,8 +125,8 @@ func IdentifyLicensesInFile(filePath string, options Options, licenseLibrary *li return IdentifierResults{}, nil } - if slices.Contains(options.Patterns, MATCH_PATTERN_SPDX_ID) { - Logger.Infof("Pattern: %s\n", MATCH_PATTERN_SPDX_ID) + if slices.Contains(options.Patterns, configurer.MATCH_PATTERN_SPDX_ID) { + Logger.Infof("Pattern: %s\n", configurer.MATCH_PATTERN_SPDX_ID) licenseMatches, err := findSPDXIdentifierInFile(filePath, 10) if err != nil { return IdentifierResults{}, err