1package bug
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/MichaelMure/git-bug/repository"
9 "github.com/MichaelMure/git-bug/util/text"
10)
11
12type Person struct {
13 Name string `json:"name"`
14 Email string `json:"email"`
15}
16
17// GetUser will query the repository for user detail and build the corresponding Person
18func GetUser(repo repository.Repo) (Person, error) {
19 name, err := repo.GetUserName()
20 if err != nil {
21 return Person{}, err
22 }
23 if name == "" {
24 return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.name \"John Doe\"`")
25 }
26
27 email, err := repo.GetUserEmail()
28 if err != nil {
29 return Person{}, err
30 }
31 if email == "" {
32 return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.email johndoe@example.com`")
33 }
34
35 return Person{Name: name, Email: email}, nil
36}
37
38// Match tell is the Person match the given query string
39func (p Person) Match(query string) bool {
40 return strings.Contains(strings.ToLower(p.Name), strings.ToLower(query))
41}
42
43func (p Person) Validate() error {
44 if text.Empty(p.Name) {
45 return fmt.Errorf("name is not set")
46 }
47
48 if strings.Contains(p.Name, "\n") {
49 return fmt.Errorf("name should be a single line")
50 }
51
52 if !text.Safe(p.Name) {
53 return fmt.Errorf("name is not fully printable")
54 }
55
56 if strings.Contains(p.Email, "\n") {
57 return fmt.Errorf("email should be a single line")
58 }
59
60 if !text.Safe(p.Email) {
61 return fmt.Errorf("email is not fully printable")
62 }
63
64 return nil
65}
66
67func (p Person) String() string {
68 return fmt.Sprintf("%s <%s>", p.Name, p.Email)
69}