cerrado @ fb45f1f5002ffdb40150333c5a48458b801f3022

 1package service
 2
 3import (
 4	"path"
 5
 6	"git.gabrielgio.me/cerrado/pkg/config"
 7	"git.gabrielgio.me/cerrado/pkg/git"
 8)
 9
10type (
11	Repository struct {
12		Name              string
13		Title             string
14		LastCommitMessage string
15		LastCommitDate    string
16	}
17
18	GitService struct {
19		configRepo configurationRepository
20	}
21
22	configurationRepository interface {
23		List() []*config.GitRepositoryConfiguration
24	}
25)
26
27// TODO: make it configurable
28const timeFormat = "2006.01.02 15:04:05"
29
30func NewGitService(configRepo configurationRepository) *GitService {
31	return &GitService{
32		configRepo: configRepo,
33	}
34}
35
36func (g *GitService) ListRepositories() ([]*Repository, error) {
37	rs := g.configRepo.List()
38
39	repos := make([]*Repository, len(rs))
40	for i, r := range rs {
41		repo := git.NewGitRepository(r.Path)
42		obj, err := repo.LastCommit()
43		if err != nil {
44			return nil, err
45		}
46
47		baseName := path.Base(r.Path)
48		repos[i] = &Repository{
49			Name:              baseName,
50			Title:             baseName,
51			LastCommitMessage: obj.Message,
52			LastCommitDate:    obj.Author.When.Format(timeFormat),
53		}
54	}
55
56	return repos, nil
57}