Skip to content

Commit

Permalink
Add help for the --patterns flag
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Rutkowski <[email protected]>
  • Loading branch information
mrutkows committed Jun 10, 2024
1 parent 639ae2f commit 4d4d9de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions configurer/configurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"path"
"path/filepath"
"runtime"
"strings"

"github.com/spf13/pflag"

"github.com/spf13/viper"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
21 changes: 11 additions & 10 deletions identifier/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4d4d9de

Please sign in to comment.