Add option to set the maximum number of tags to watch for an item if watch_repo is enabled

This commit is contained in:
CrazyMax
2019-06-08 19:42:19 +02:00
parent 7f87602672
commit 00ed9d24e2
6 changed files with 34 additions and 7 deletions

View File

@@ -8,20 +8,30 @@ import (
type Tags []string
// Tags returns tags of a Docker repository
func (c *RegistryClient) Tags(image registry.Image) (Tags, error) {
func (c *RegistryClient) Tags(image registry.Image, max int) (Tags, int, error) {
ctx, cancel := c.timeoutContext()
defer cancel()
imgCls, err := c.newImage(ctx, image.String())
if err != nil {
return nil, err
return nil, 0, err
}
defer imgCls.Close()
tags, err := docker.GetRepositoryTags(ctx, c.sysCtx, imgCls.Reference())
if err != nil {
return nil, err
return nil, 0, err
}
return Tags(tags), err
// Reverse order (latest tags first)
for i := len(tags)/2 - 1; i >= 0; i-- {
opp := len(tags) - 1 - i
tags[i], tags[opp] = tags[opp], tags[i]
}
if max > 0 && len(tags) >= max {
return Tags(tags[:max]), len(tags), nil
}
return Tags(tags), len(tags), nil
}