diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-05-26 20:33:37 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-05-26 20:33:37 +0200 |
commit | 4534dffb865eb1a50bfbc291a5c3798183081caf (patch) | |
tree | d5bd1a2d9912a6442e3be1511ffb1d99f12287b0 /pkg | |
parent | 349a3d1ff36a436261b1b65b870f8f262f06584f (diff) | |
download | cerrado-4534dffb865eb1a50bfbc291a5c3798183081caf.tar.gz cerrado-4534dffb865eb1a50bfbc291a5c3798183081caf.tar.bz2 cerrado-4534dffb865eb1a50bfbc291a5c3798183081caf.zip |
feat: Add actual git listing implementation
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/git/git.go | 82 | ||||
-rw-r--r-- | pkg/handler/git.go | 9 | ||||
-rw-r--r-- | pkg/handler/status.go | 2 | ||||
-rw-r--r-- | pkg/service/git.go | 51 | ||||
-rw-r--r-- | pkg/u/file.go | 21 | ||||
-rw-r--r-- | pkg/u/list.go (renamed from pkg/u/util.go) | 0 | ||||
-rw-r--r-- | pkg/u/list_test.go (renamed from pkg/u/util_test.go) | 0 |
7 files changed, 149 insertions, 16 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go new file mode 100644 index 0000000..85a3b95 --- /dev/null +++ b/pkg/git/git.go @@ -0,0 +1,82 @@ +package git + +import ( + "errors" + "os" + "path" + + "git.gabrielgio.me/cerrado/pkg/u" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" +) + +var ( + ScanPathErr = errors.New("Scan path does not exist") + RepoPathErr = errors.New("Repository path does not exist") + missingHeadErr = errors.New("Head not found") +) + +type ( + GitServerRepository struct { + scanPath string + } + + GitRepository struct { + path string + } +) + +func NewGitServerRepository(scanPath string) *GitServerRepository { + return &GitServerRepository{scanPath} +} + +func NewGitRepository(dir string) *GitRepository { + return &GitRepository{ + path: dir, + } +} + +func (g *GitServerRepository) List() ([]*GitRepository, error) { + if !u.FileExist(g.scanPath) { + return nil, ScanPathErr + } + + entries, err := os.ReadDir(g.scanPath) + if err != nil { + return nil, err + } + + repos := make([]*GitRepository, 0) + for _, e := range entries { + if !e.IsDir() { + continue + } + + fullPath := path.Join(g.scanPath, e.Name()) + repos = append(repos, NewGitRepository(fullPath)) + } + + return repos, nil +} + +func (g *GitRepository) Path() string { + return g.path +} + +func (g *GitRepository) LastCommit() (*object.Commit, error) { + repo, err := git.PlainOpen(g.path) + if err != nil { + return nil, err + } + + ref, err := repo.Head() + if err != nil { + return nil, errors.Join(missingHeadErr, err) + } + + c, err := repo.CommitObject(ref.Hash()) + if err != nil { + return nil, err + } + return c, nil +} diff --git a/pkg/handler/git.go b/pkg/handler/git.go index 5b09121..1ed2c49 100644 --- a/pkg/handler/git.go +++ b/pkg/handler/git.go @@ -1,6 +1,7 @@ package handler import ( + "log/slog" "net/http" "git.gabrielgio.me/cerrado/pkg/service" @@ -16,6 +17,12 @@ func NewGitHandler(gitService *service.GitService) *GitHandler { } func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { - gitList := &templates.GitListPage{g.gitService.ListRepositories()} + repos, err := g.gitService.ListRepositories() + if err != nil { + slog.Error("Error listing repo", "error", err) + return + } + + gitList := &templates.GitListPage{repos} templates.WritePageTemplate(w, gitList) } diff --git a/pkg/handler/status.go b/pkg/handler/status.go index 2a84a7e..1ca7f70 100644 --- a/pkg/handler/status.go +++ b/pkg/handler/status.go @@ -19,7 +19,7 @@ func ConfigFile(configPath string) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, _ *http.Request) { f, err := os.Open(configPath) if err != nil { - slog.Error("Error openning config file json", "error", err, "path", configPath) + slog.Error("Error openning config file", "error", err, "path", configPath) return } diff --git a/pkg/service/git.go b/pkg/service/git.go index 0415cee..94ca75e 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -1,30 +1,53 @@ package service -import "fmt" +import ( + "path" + + "git.gabrielgio.me/cerrado/pkg/git" +) type ( - GitService struct{} + GitService struct { + server *git.GitServerRepository + } Repository struct { - Name string - Title string - Description string + Name string + Title string + LastCommitMessage string + LastCommitDate string } ) -func NewGitService() *GitService { - return &GitService{} +// TODO: make it configurable +const timeFormat = "2006.01.02 15:04:05" + +func NewGitService(server *git.GitServerRepository) *GitService { + return &GitService{ + server: server, + } } -func (g *GitService) ListRepositories() []*Repository { - repos := make([]*Repository, 10) +func (g *GitService) ListRepositories() ([]*Repository, error) { + rs, err := g.server.List() + if err != nil { + return nil, err + } + + repos := make([]*Repository, len(rs)) + for i, r := range rs { + obj, err := r.LastCommit() + if err != nil { + return nil, err + } - for i := range 10 { + baseName := path.Base(r.Path()) repos[i] = &Repository{ - Name: fmt.Sprintf("repository-%d", i), - Title: fmt.Sprintf("Repository %d", i), - Description: fmt.Sprintf("This is a description for repository %d", i), + Name: baseName, + Title: baseName, + LastCommitMessage: obj.Message, + LastCommitDate: obj.Author.When.Format(timeFormat), } } - return repos + return repos, nil } diff --git a/pkg/u/file.go b/pkg/u/file.go new file mode 100644 index 0000000..cf86c75 --- /dev/null +++ b/pkg/u/file.go @@ -0,0 +1,21 @@ +package u + +import ( + "errors" + "log/slog" + "os" +) + +func FileExist(filename string) bool { + if _, err := os.Stat(filename); err == nil { + return true + + } else if errors.Is(err, os.ErrNotExist) { + return false + } else { + slog.Warn("Schrödinger's file: it may or may not exist", "file", filename) + // Schrodinger: file may or may not exist. To be extra safe it will + // report the file doest not exist + return false + } +} diff --git a/pkg/u/util.go b/pkg/u/list.go index 34eafd1..34eafd1 100644 --- a/pkg/u/util.go +++ b/pkg/u/list.go diff --git a/pkg/u/util_test.go b/pkg/u/list_test.go index a6d84c7..a6d84c7 100644 --- a/pkg/u/util_test.go +++ b/pkg/u/list_test.go |