person.go

 1package bug
 2
 3import (
 4	"encoding/json"
 5	"github.com/MichaelMure/git-bug/repository"
 6	"github.com/MichaelMure/git-bug/util"
 7	"github.com/pkg/errors"
 8)
 9
10type Person struct {
11	Name  string `json:"name"`
12	Email string `json:"email"`
13}
14
15// GetUser will query the repository for user detail and build the corresponding Person
16func GetUser(repo repository.Repo) (Person, error) {
17	name, err := repo.GetUserName()
18	if err != nil {
19		return Person{}, err
20	}
21	if name == "" {
22		return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.name \"John Doe\"`")
23	}
24
25	email, err := repo.GetUserEmail()
26	if err != nil {
27		return Person{}, err
28	}
29	if email == "" {
30		return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.email johndoe@example.com`")
31	}
32
33	return Person{Name: name, Email: email}, nil
34}
35
36// Store will convert the Person to JSON and store it in the internal git datastore
37// Return the git hash handle of the data
38func (person *Person) Store(repo repository.Repo) (util.Hash, error) {
39
40	data, err := json.Marshal(person)
41
42	if err != nil {
43		return "", err
44	}
45
46	return repo.StoreData(data)
47}