package git import ( "log/slog" "net/http" "git.gabrielgio.me/cerrado/pkg/service" "git.gabrielgio.me/cerrado/templates" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/gorilla/mux" ) type ( GitHandler struct { gitService gitService } gitService interface { ListRepositories() ([]*service.Repository, error) ListCommits(name string, ref string) ([]*object.Commit, error) GetHead(name string) (*plumbing.Reference, error) ListTags(name string) ([]*object.Tag, error) ListBranches(name string) ([]*plumbing.Reference, error) } ) func NewGitHandler(gitService gitService) *GitHandler { return &GitHandler{ gitService: gitService, } } func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { repos, err := g.gitService.ListRepositories() if err != nil { slog.Error("Error listing repo", "error", err) return } gitList := &templates.GitListPage{repos} templates.WritePageTemplate(w, gitList) } func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] ref, err := g.gitService.GetHead(name) if err != nil { slog.Error("Error loading head", "error", err) return } gitList := &templates.GitItemPage{ Name: name, Ref: ref.Name().Short(), GitItemBase: &templates.GitItemSummaryPage{}, } templates.WritePageTemplate(w, gitList) } func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] ref, err := g.gitService.GetHead(name) if err != nil { slog.Error("Error loading head", "error", err) return } gitList := &templates.GitItemPage{ Name: name, Ref: ref.Name().Short(), GitItemBase: &templates.GitItemAboutPage{}, } templates.WritePageTemplate(w, gitList) } func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] tags, err := g.gitService.ListTags(name) if err != nil { slog.Error("Error loading tags", "error", err) return } branches, err := g.gitService.ListBranches(name) if err != nil { slog.Error("Error loading branches", "error", err) return } ref, err := g.gitService.GetHead(name) if err != nil { slog.Error("Error loading head", "error", err) return } gitList := &templates.GitItemPage{ Name: name, Ref: ref.Name().Short(), GitItemBase: &templates.GitItemRefsPage{ Tags: tags, Branches: branches, }, } templates.WritePageTemplate(w, gitList) } func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] ref := mux.Vars(r)["ref"] gitList := &templates.GitItemPage{ Name: name, Ref: ref, GitItemBase: &templates.GitItemTreePage{}, } templates.WritePageTemplate(w, gitList) } func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] ref := mux.Vars(r)["ref"] commits, err := g.gitService.ListCommits(name, ref) if err != nil { slog.Error("Error loading commits", "error", err) return } gitList := &templates.GitItemPage{ Name: name, Ref: ref, GitItemBase: &templates.GitItemLogPage{ Commits: commits, }, } templates.WritePageTemplate(w, gitList) }