This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
/
coauthor.go
80 lines (66 loc) · 1.82 KB
/
coauthor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package lib
import (
"fmt"
"regexp"
"strings"
)
type CoAuthor struct {
Name string
Email string
}
func newCoAuthor(name string, email string) CoAuthor {
return CoAuthor{Name: name, Email: email}
}
func (c CoAuthor) String() string {
return fmt.Sprintf("Co-authored-by: %s", c.UserID())
}
func (c CoAuthor) UserID() string {
return fmt.Sprintf("%s <%s>", c.Name, c.Email)
}
type CoAuthors []CoAuthor
func (authors CoAuthors) String() string {
var coAuthorStrings []string
for _, author := range authors {
coAuthorStrings = append(coAuthorStrings, author.UserID())
}
return strings.Join(coAuthorStrings, "\n")
}
func (authors CoAuthors) Get(name string) (CoAuthor, error) {
for _, author := range authors {
if author.Name == name {
return author, nil
}
}
validNames := strings.Join(authors.Names(), ", ")
return CoAuthor{}, fmt.Errorf("author %q is not specified in the authors file. valid options: [%s]", name, validNames)
}
func (authors CoAuthors) Names() []string {
names := []string{} // this is instantiated so that it won't be nil.
for _, author := range authors {
names = append(names, author.Name)
}
return names
}
func (authors CoAuthors) Any() bool {
return len(authors) > 0
}
func (authors CoAuthors) Subset(names []string) []CoAuthor {
var subset CoAuthors
for _, name := range names {
if author, err := authors.Get(name); err == nil {
subset = append(subset, author)
}
}
return subset
}
func (authors *CoAuthors) From(bytes []byte) error {
rxp := regexp.MustCompile(`(.*) <(.*)>`)
matchGroups := rxp.FindAllStringSubmatch(string(bytes), -1)
for _, group := range matchGroups {
if len(group) != 3 {
return fmt.Errorf("invalid author format: %s. Should be like: Name <email@example.com>", group[0])
}
*authors = append(*authors, newCoAuthor(group[1], group[2]))
}
return nil
}