aboutsummaryrefslogtreecommitdiff
path: root/match_copyright.go
diff options
context:
space:
mode:
Diffstat (limited to 'match_copyright.go')
-rw-r--r--match_copyright.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/match_copyright.go b/match_copyright.go
new file mode 100644
index 0000000..aa35950
--- /dev/null
+++ b/match_copyright.go
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-3.0-only
+// SPDX-FileCopyrightText: 2026 M. Shulhan <ms@kilabit.info>
+
+package spdxconv
+
+import (
+ "fmt"
+ "regexp"
+)
+
+type matchCopyright struct {
+ rePattern *regexp.Regexp
+
+ // Pattern to be searched in file.
+ Pattern string `ini:"match-copyright::pattern"`
+
+ year string
+ author string
+ contact string
+}
+
+func (cmc *matchCopyright) init() (err error) {
+ var logp = `match-copyright`
+ if cmc.Pattern != `` {
+ cmc.rePattern, err = regexp.Compile(cmc.Pattern)
+ if err != nil {
+ return fmt.Errorf(`%s: pattern %q: %w`, logp, cmc.Pattern, err)
+ }
+ }
+ return nil
+}
+
+func (cmc *matchCopyright) match(line string) bool {
+ matches := cmc.rePattern.FindStringSubmatch(line)
+ if len(matches) == 0 {
+ return false
+ }
+ namedMatches := map[string]string{}
+ for x, name := range cmc.rePattern.SubexpNames() {
+ if x != 0 && name != `` {
+ namedMatches[name] = matches[x]
+ }
+ }
+ cmc.year = namedMatches[`year`]
+ cmc.author = namedMatches[`author`]
+ cmc.contact = namedMatches[`contact`]
+ return len(namedMatches) >= 1
+}
+
+func (cmc *matchCopyright) String() string {
+ return fmt.Sprintf(`%s <%s>`, cmc.author, cmc.contact)
+}