store user info in the datastore

Michael Muré created

Change summary

bug/bug.go         |  1 +
bug/person.go      | 19 +++++++++++++++++--
commands/new.go    |  2 ++
repository/git.go  | 11 +++++++++++
repository/repo.go |  5 +++++
5 files changed, 36 insertions(+), 2 deletions(-)

Detailed changes

bug/bug.go 🔗

@@ -4,3 +4,4 @@ type Bug struct {
 	Title    string
 	Comments []Comment
 }
+

bug/person.go 🔗

@@ -1,15 +1,17 @@
 package bug
 
 import (
+	"encoding/json"
 	"github.com/MichaelMure/git-bug/repository"
 	"github.com/pkg/errors"
 )
 
 type Person struct {
-	Name  string
-	Email string
+	Name  string `json:"name"`
+	Email string `json:"email"`
 }
 
+// GetUser will query the repository for user detail and build the corresponding Person
 func GetUser(repo repository.Repo) (Person, error) {
 	name, err := repo.GetUserName()
 	if err != nil {
@@ -29,3 +31,16 @@ func GetUser(repo repository.Repo) (Person, error) {
 
 	return Person{Name: name, Email: email}, nil
 }
+
+// Store will convert the Person to JSON and store it in the internal git datastore
+// Return the git hash handle of the data
+func (person *Person) Store(repo repository.Repo) (repository.Hash, error) {
+
+	data, err := json.Marshal(person)
+
+	if err != nil {
+		return "", err
+	}
+
+	return repo.StoreData(data)
+}

commands/new.go 🔗

@@ -62,6 +62,8 @@ func newBug(repo repository.Repo, args []string) error {
 
 	fmt.Println(bug)
 
+	author.Store(repo)
+
 	return nil
 
 }

repository/git.go 🔗

@@ -114,6 +114,17 @@ func (repo *GitRepo) PushRefs(remote string, refPattern string) error {
 	return nil
 }
 
+// StoreData will store arbitrary data and return the corresponding hash
+func (repo *GitRepo) StoreData(data []byte) (Hash, error) {
+	var stdin = bytes.NewReader(data)
+	var stdout bytes.Buffer
+	var stderr bytes.Buffer
+
+	err := repo.runGitCommandWithIO(stdin, &stdout, &stderr, "hash-object", "--stdin", "-w")
+
+	return Hash(stdout.String()), err
+}
+
 /*
 //
 //// GetSubmitStrategy returns the way in which a review is submitted

repository/repo.go 🔗

@@ -1,6 +1,8 @@
 // Package repository contains helper methods for working with a Git repo.
 package repository
 
+type Hash string
+
 // Repo represents a source code repository.
 type Repo interface {
 	// GetPath returns the path to the repo.
@@ -20,4 +22,7 @@ type Repo interface {
 
 	// PushRefs push git refs to a remote
 	PushRefs(remote string, refPattern string) error
+
+	// StoreData will store arbitrary data and return the corresponding hash
+	StoreData([]byte) (Hash, error)
 }