Skip to content

Commit

Permalink
server: add sort option
Browse files Browse the repository at this point in the history
This adds a '--sort' to the 'server' sub-command. Currently there is
only one sort option, which is by create date. I assumed that his is
useful in general, compared to a random order, so I made this the
default.

This is slightly over engineered, but makes it easier to add additional
sorting options later. One that comes to my mind is sorting by tag (some
kind of natsort/semversort).

Signed-off-by: Roland Kammerer <[email protected]>
  • Loading branch information
rck committed Apr 23, 2019
1 parent d959057 commit e959cb8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
Expand All @@ -28,6 +29,7 @@ type registryController struct {
l sync.Mutex
tmpl *template.Template
generateOnly bool
repoSortBy string
}

type v1Compatibility struct {
Expand All @@ -44,6 +46,16 @@ type Repository struct {
VulnerabilityReport clair.VulnerabilityReport `json:"vulnerability"`
}

const (
sortByCreateDate = "create-date"
)

type repoByDate []Repository

func (r repoByDate) Len() int { return len(r) }
func (r repoByDate) Less(i, j int) bool { return r[i].Created.Before(r[j].Created) }
func (r repoByDate) Swap(i, j int) { r[i], r[j] = r[j], r[i] }

// An AnalysisResult holds all vulnerabilities of a scan
type AnalysisResult struct {
Repositories []Repository `json:"repositories"`
Expand Down Expand Up @@ -228,6 +240,11 @@ func (rc *registryController) generateTagsTemplate(ctx context.Context, repo str
result.Repositories = append(result.Repositories, rp)
}

switch rc.repoSortBy {
case sortByCreateDate:
sort.Sort(sort.Reverse(repoByDate(result.Repositories)))
}

// Execute the template.
var buf bytes.Buffer
if err := rc.tmpl.ExecuteTemplate(&buf, "tags", result); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (cmd *serverCommand) Register(fs *flag.FlagSet) {
fs.StringVar(&cmd.assetPath, "asset-path", "", "Path to assets and templates")

fs.BoolVar(&cmd.generateAndExit, "once", false, "generate the templates once and then exit")

sortOpts := []string{sortByCreateDate}
fs.StringVar(&cmd.repoSortBy, "sort", sortByCreateDate,
fmt.Sprintf("generate tag templates sorted by property (one of: %s)", strings.Join(sortOpts, ",")))
}

type serverCommand struct {
Expand All @@ -57,6 +61,7 @@ type serverCommand struct {
listenAddress string
port string
assetPath string
repoSortBy string
}

func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
Expand All @@ -66,10 +71,15 @@ func (cmd *serverCommand) Run(ctx context.Context, args []string) error {
return err
}

if cmd.repoSortBy != sortByCreateDate {
return fmt.Errorf("sort option invalid; '%s' is not valid", cmd.repoSortBy)
}

// Create the registry controller for the handlers.
rc := registryController{
reg: r,
generateOnly: cmd.generateAndExit,
repoSortBy: cmd.repoSortBy,
}

// Create a clair client if the user passed in a server address.
Expand Down

0 comments on commit e959cb8

Please sign in to comment.