Skip to content

Commit

Permalink
new: sort tags by date
Browse files Browse the repository at this point in the history
  • Loading branch information
fntlnz committed Jul 24, 2024
1 parent d35d893 commit 03c87b5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
32 changes: 30 additions & 2 deletions pkg/filler/filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log/slog"
"sort"
"time"

"github.com/regclient/regclient"
Expand Down Expand Up @@ -82,14 +83,41 @@ func (f *Filler) RepoData(ctx context.Context, repo string) (*templates.Reposito
log.Warn("could not generate tag data", logger.ErrAttr(err), slog.String("tag", tag))
continue
}
if tagData == nil {
continue
}
tags = append(tags, *tagData)
}
if len(tags) == 0 {
return nil, nil
}

orderedTags := orderTagsByDate(tags)
if len(orderedTags) == 0 {
return nil, nil
}
mostRecentTag := orderedTags[0]
repoData := &templates.RepositoryData{
BaseData: baseData,
RepositoryName: repo,
PullReference: repoRef.CommonName(),
Tags: tags,
PullReference: mostRecentTag.PullReference,
Tags: orderedTags,
}

return repoData, err
}

func orderTagsByDate(tags []templates.TagData) []templates.TagData {
sort.Slice(tags, func(i, j int) bool {
dateI, err := time.Parse(time.RFC3339, tags[i].CreatedAt)
if err != nil {
return false
}
dateJ, err := time.Parse(time.RFC3339, tags[j].CreatedAt)
if err != nil {
return false
}
return dateI.After(dateJ)
})
return tags
}
3 changes: 3 additions & 0 deletions pkg/server/staticreg/staticreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func (s *StaticregServer) RepositoriesListHandler(c *gin.Context) {
if err != nil {
log.Warn("could not retrieve repo data", slog.String("repo", repo), logger.ErrAttr(err))
}
if repoData == nil {
continue
}
repositoriesData = append(repositoriesData, *repoData)
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/templates/tmpl/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ <h1 class="text-3xl font-bold tracking-tight text-gray-900">{{.RegistryName}}</h
<td class="p-2"><a class="text-blue-600 hover:text-blue-800 visited:text-purple-600"
href="{{$.AbsoluteDir}}repo/{{.RepositoryName}}">{{.RepositoryName}}</a>
</td>
<td class="p-2 font-mono text-sm">docker pull {{.PullReference}}</td>
<td class="p-2 font-mono text-sm">docker pull {{.PullReference}}

</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</main>

<footer class="bg-gray-200 h-screen p-4 text-center text-sm text-gray-600">
<footer class="bg-gray-200 p-4 text-center text-sm text-gray-600">
<p>Last updated: {{.LastUpdated}}</p>
</footer>

Expand Down
11 changes: 7 additions & 4 deletions pkg/templates/tmpl/repository.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
<title>{{.RepositoryName}} | {{.RegistryName}}</title>
</head>

<body class="bg-gray-100 h-full">
<header class="bg-white shadow">
<div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
<body class="bg-gray-100 h-screen w-screen">
<header class="bg-white shadow flex justify-between flex-wrap bg-teal w-screen">
<div class="mx-auto px-4 py-6 sm:px-6 lg:px-8">
<h1 class="text-3xl font-bold tracking-tight text-gray-900"><a
class="text-blue-600 hover:text-blue-800 visited:text-purple-600"
href="{{.AbsoluteDir}}">{{.RegistryName}}</a>/{{.RepositoryName}}</h1>
</div>

</header>


<main>
<div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">
<table class="w-full bg-white border">
Expand All @@ -42,7 +45,7 @@ <h1 class="text-3xl font-bold tracking-tight text-gray-900"><a
</table>
</div>
</main>
<footer class="bg-gray-200 h-screen p-4 text-center text-sm text-gray-600">
<footer class="p-4 text-center text-sm text-gray-600">
<p>Last updated: {{.LastUpdated}}</p>
</footer>
</body>
Expand Down

0 comments on commit 03c87b5

Please sign in to comment.