summaryrefslogtreecommitdiffstats
path: root/routes/routes.go
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2024-02-19 22:23:39 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2024-02-19 22:23:39 +0000
commit3c1b8007b6d5992e6bffc701ad06e0adec717145 (patch)
treeaf2a9a756aa6ad8e8f3f8ac18273c1668c711afe /routes/routes.go
parentbdfc973207a67a3b217c130520d53373d088763c (diff)
downloadlegit-3c1b8007b6d5992e6bffc701ad06e0adec717145.tar.gz
legit-3c1b8007b6d5992e6bffc701ad06e0adec717145.tar.bz2
legit-3c1b8007b6d5992e6bffc701ad06e0adec717145.zip
support categories at index
Diffstat (limited to '')
-rw-r--r--routes/routes.go95
1 files changed, 82 insertions, 13 deletions
diff --git a/routes/routes.go b/routes/routes.go
index 0499a9e..ca2e729 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -1,6 +1,7 @@
package routes
import (
+ "errors"
"fmt"
"html/template"
"log"
@@ -8,12 +9,14 @@ import (
"os"
"path/filepath"
"sort"
+ "strings"
"time"
"git.icyphox.sh/legit/config"
"git.icyphox.sh/legit/git"
"github.com/alexedwards/flow"
"github.com/dustin/go-humanize"
+ gogit "github.com/go-git/go-git/v5"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
@@ -22,6 +25,11 @@ type deps struct {
c *config.Config
}
+type info struct {
+ Name, Path, Desc, Idle string
+ d time.Time
+}
+
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
dirs, err := os.ReadDir(d.c.Repo.ScanPath)
if err != nil {
@@ -30,12 +38,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
return
}
- type info struct {
- Name, Desc, Idle string
- d time.Time
- }
-
- infos := []info{}
+ categories := make(map[string][]info)
for _, dir := range dirs {
if d.isIgnored(dir.Name()) {
@@ -44,7 +47,17 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(d.c.Repo.ScanPath, dir.Name())
gr, err := git.Open(path, "")
- if err != nil {
+ if errors.Is(err, gogit.ErrRepositoryNotExists) {
+ log.Printf("reading category: %s", dir.Name())
+ infos, err := d.IndexCategory(path, dir.Name())
+ if err != nil {
+ log.Printf("reading category: %s", err)
+ }
+ if len(infos) > 0 {
+ categories[dir.Name()] = infos
+ }
+ continue
+ } else if err != nil {
log.Println(err)
continue
}
@@ -57,25 +70,29 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
}
desc := getDescription(path)
+ name := strings.TrimSuffix(dir.Name(), ".git")
- infos = append(infos, info{
- Name: dir.Name(),
+ categories[""] = append(categories[""], info{
+ Name: name,
+ Path: name,
Desc: desc,
Idle: humanize.Time(c.Author.When),
d: c.Author.When,
})
}
- sort.Slice(infos, func(i, j int) bool {
- return infos[j].d.Before(infos[i].d)
- })
+ for _, infos := range categories {
+ sort.Slice(infos, func(i, j int) bool {
+ return infos[j].d.Before(infos[i].d)
+ })
+ }
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["meta"] = d.c.Meta
- data["info"] = infos
+ data["categories"] = categories
if err := t.ExecuteTemplate(w, "index", data); err != nil {
log.Println(err)
@@ -83,6 +100,58 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
}
}
+func (d *deps) IndexCategory(scanpath string, category string) ([]info, error) {
+ dirs, err := os.ReadDir(scanpath)
+ if err != nil {
+ return nil, fmt.Errorf("reading scan path: %s", err)
+ }
+
+ infos := []info{}
+
+ for _, dir := range dirs {
+ if d.isIgnored(dir.Name()) {
+ continue
+ }
+
+ path := filepath.Join(scanpath, dir.Name())
+ gr, err := git.Open(path, "")
+ if errors.Is(err, gogit.ErrRepositoryNotExists) {
+ log.Println(path)
+ folder_infos, err := d.IndexCategory(path, category)
+ if err != nil {
+ log.Println(err)
+ continue
+ }
+ infos = append(infos, folder_infos...)
+ continue
+ } else if err != nil {
+ log.Println(err)
+ continue
+ }
+
+ c, err := gr.LastCommit()
+ if err != nil {
+ return nil, err
+ }
+
+ desc := getDescription(path)
+ repodir := filepath.Join(scanpath, dir.Name())
+ repopath := strings.Split(repodir, category)
+ name := strings.TrimSuffix(repopath[len(repopath)-1], ".git")
+ name = strings.TrimPrefix(name, "/")
+
+ infos = append(infos, info{
+ Name: name,
+ Path: category + "/" + name,
+ Desc: desc,
+ Idle: humanize.Time(c.Author.When),
+ d: c.Author.When,
+ })
+ }
+
+ return infos, nil
+}
+
func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name")
if d.isIgnored(name) {