aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.example.scfg8
-rw-r--r--pkg/config/config.go49
-rw-r--r--pkg/handler/git/handler.go28
3 files changed, 84 insertions, 1 deletions
diff --git a/config.example.scfg b/config.example.scfg
index 59a04ad..3ba8959 100644
--- a/config.example.scfg
+++ b/config.example.scfg
@@ -10,6 +10,14 @@ syntax-highlight monokailight
passphrase $2a$14$VnB/ZcB1DUDkMnosRA6Y7.dj8h5eroslDxTeXlLwfQX/x86mh6WAq
aes-key 8XHptZxSWCGs1m7QzztX5zNQ7D9NiQevVX0DaUTNMbDpRwFzoJiB0U7K6O/kqIt01jJVgzBUfiR8ES46ZLLb4w==
+# order of the list of repository, values:
+# unordered
+# alphabetical-asc
+# alphabetical-desc
+# lastcommit-asc
+# lastcommit-desc
+order-by lastcommit-asc
+
# repository section is order dependent where the first repository has priority
# in case of conflict. Repository has also priority over scan. The order
# between scan and repository is irrelevant which means that all repository
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 1ad5108..60a3444 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
+ "log/slog"
"os"
"path"
"path/filepath"
@@ -19,6 +20,16 @@ var (
ErrInvalidProperty = errors.New("Invalid property")
)
+type OrderBy int
+
+const (
+ Unordered OrderBy = iota
+ AlphabeticalAsc
+ AlphabeticalDesc
+ LastCommitAsc
+ LastCommitDesc
+)
+
type (
// scan represents piece of the scan from the configuration file.
@@ -36,6 +47,7 @@ type (
Passphrase string
SyntaxHighlight string
AESKey string
+ OrderBy string
Repositories []*GitRepositoryConfiguration
}
@@ -58,6 +70,7 @@ type (
passphrase []byte
aesKey []byte
syntaxHighlight string
+ orderBy OrderBy
repositories []*GitRepositoryConfiguration
}
)
@@ -80,6 +93,7 @@ func LoadConfigurationRepository(configPath string) (*ConfigurationRepository, e
repositories: config.Repositories,
rootReadme: config.RootReadme,
syntaxHighlight: config.SyntaxHighlight,
+ orderBy: parseOrderBy(config.OrderBy),
}
for _, scan := range config.Scans {
@@ -99,6 +113,10 @@ func (c *ConfigurationRepository) GetRootReadme() string {
return c.rootReadme
}
+func (c *ConfigurationRepository) GetOrderBy() OrderBy {
+ return c.orderBy
+}
+
func (c *ConfigurationRepository) GetSyntaxHighlight() string {
return c.syntaxHighlight
}
@@ -211,6 +229,11 @@ func parse(r io.Reader) (*configuration, error) {
return nil, err
}
+ err = setOrderby(block, &config.OrderBy)
+ if err != nil {
+ return nil, err
+ }
+
err = setRepositories(block, &config.Repositories)
if err != nil {
return nil, err
@@ -316,6 +339,11 @@ func setSyntaxHighlight(block scfg.Block, listenAddr *string) error {
return setString(scanDir, listenAddr)
}
+func setOrderby(block scfg.Block, orderBy *string) error {
+ scanDir := block.Get("order-by")
+ return setString(scanDir, orderBy)
+}
+
func setListenAddr(block scfg.Block, listenAddr *string) error {
scanDir := block.Get("listen-addr")
return setString(scanDir, listenAddr)
@@ -363,3 +391,24 @@ func setString(dir *scfg.Directive, field *string) error {
}
return nil
}
+
+func parseOrderBy(s string) OrderBy {
+ switch s {
+ case "":
+ return LastCommitAsc
+ case "unordered":
+ return Unordered
+ case "alphabetical-asc":
+ return AlphabeticalAsc
+ case "alphabetical-desc":
+ return AlphabeticalDesc
+ case "lastcommit-asc":
+ return LastCommitAsc
+ case "lastcommit-desc":
+ return LastCommitDesc
+ default:
+ slog.Warn("Invalid order-by using default unordered")
+ return LastCommitAsc
+
+ }
+}
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index 436d364..9c7ba5b 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -9,8 +9,10 @@ import (
"net/http"
"os"
"path/filepath"
+ "sort"
"strings"
+ "git.gabrielgio.me/cerrado/pkg/config"
"git.gabrielgio.me/cerrado/pkg/ext"
"git.gabrielgio.me/cerrado/pkg/service"
"git.gabrielgio.me/cerrado/pkg/u"
@@ -34,6 +36,7 @@ type (
configurationRepository interface {
GetRootReadme() string
GetSyntaxHighlight() string
+ GetOrderBy() config.OrderBy
}
)
@@ -79,7 +82,7 @@ func (g *GitHandler) List(w http.ResponseWriter, r *http.Request) error {
bs = markdown.Render(doc, renderer)
gitList := &templates.GitListPage{
- Respositories: repos,
+ Respositories: orderBy(repos, g.config.GetOrderBy()),
About: bs,
}
templates.WritePageTemplate(w, gitList, r.Context())
@@ -390,3 +393,26 @@ func GetLexers(filename string) chroma.Lexer {
func isPublic(r *service.Repository) bool {
return r.Public
}
+
+func orderBy(repos []*service.Repository, order config.OrderBy) []*service.Repository {
+ switch order {
+ case config.AlphabeticalAsc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].Name < repos[j].Name
+ })
+ case config.AlphabeticalDesc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].Name > repos[j].Name
+ })
+ case config.LastCommitAsc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].LastCommit.Committer.When.Before(repos[j].LastCommit.Committer.When)
+ })
+ case config.LastCommitDesc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].LastCommit.Committer.When.After(repos[j].LastCommit.Committer.When)
+ })
+ }
+
+ return repos
+}