diff options
Diffstat (limited to 'match_file_comment.go')
| -rw-r--r-- | match_file_comment.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/match_file_comment.go b/match_file_comment.go new file mode 100644 index 0000000..ac2db29 --- /dev/null +++ b/match_file_comment.go @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info> + +package spdxconv + +import ( + "fmt" + "regexp" +) + +// matchFileComment define the configuration to set the file comment prefix +// and suffix based on pattern on file name. +// +// File name that have empty Prefix and Suffix and match with the pattern will +// have the ".license" file created. +type matchFileComment struct { + rePattern *regexp.Regexp + + Pattern string `ini:"match-file-comment::pattern"` + Prefix string `ini:"match-file-comment::prefix"` + Suffix string `ini:"match-file-comment::suffix"` +} + +func (mfc *matchFileComment) init() (err error) { + var logp = `match-file-comment` + + mfc.rePattern, err = regexp.Compile(mfc.Pattern) + if err != nil { + return fmt.Errorf(`%s: pattern %q: %w`, logp, mfc.Pattern, err) + } + if mfc.Prefix != `` && mfc.Prefix[len(mfc.Prefix)-1] != ' ' { + mfc.Prefix += ` ` + } + if mfc.Suffix != `` && mfc.Suffix[0] != ' ' { + mfc.Suffix = ` ` + mfc.Suffix + } + return nil +} + +func (mfc *matchFileComment) isDirectLicense() bool { + return mfc.Prefix == `` && mfc.Suffix == `` +} |
