forked from TrueCloudLab/distribution
f3207e76c8
This change adds a basic catalog endpoint to the API, which returns a list, or partial list, of all of the repositories contained in the registry. Calls to this endpoint are somewhat expensive, as every call requires walking a large part of the registry. Instead, to maintain a list of repositories, you would first call the catalog endpoint to get an initial list, and then use the events API to maintain any future repositories. Signed-off-by: Patrick Devine <patrick.devine@docker.com>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
"github.com/docker/distribution"
|
|
"github.com/docker/distribution/context"
|
|
storageDriver "github.com/docker/distribution/registry/storage/driver"
|
|
)
|
|
|
|
type catalogSvc struct {
|
|
ctx context.Context
|
|
driver storageDriver.StorageDriver
|
|
}
|
|
|
|
var _ distribution.CatalogService = &catalogSvc{}
|
|
|
|
// Get returns a list, or partial list, of repositories in the registry.
|
|
// Because it's a quite expensive operation, it should only be used when building up
|
|
// an initial set of repositories.
|
|
func (c *catalogSvc) Get(maxEntries int, lastEntry string) ([]string, bool, error) {
|
|
log.Infof("Retrieving up to %d entries of the catalog starting with '%s'", maxEntries, lastEntry)
|
|
var repos []string
|
|
|
|
root, err := defaultPathMapper.path(repositoriesRootPathSpec{})
|
|
if err != nil {
|
|
return repos, false, err
|
|
}
|
|
|
|
Walk(c.ctx, c.driver, root, func(fileInfo storageDriver.FileInfo) error {
|
|
filePath := fileInfo.Path()
|
|
|
|
// lop the base path off
|
|
repoPath := filePath[len(root)+1:]
|
|
|
|
_, file := path.Split(repoPath)
|
|
if file == "_layers" {
|
|
repoPath = strings.TrimSuffix(repoPath, "/_layers")
|
|
if repoPath > lastEntry {
|
|
repos = append(repos, repoPath)
|
|
}
|
|
return ErrSkipDir
|
|
} else if strings.HasPrefix(file, "_") {
|
|
return ErrSkipDir
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
sort.Strings(repos)
|
|
|
|
moreEntries := false
|
|
if len(repos) > maxEntries {
|
|
moreEntries = true
|
|
repos = repos[0:maxEntries]
|
|
}
|
|
|
|
return repos, moreEntries, nil
|
|
}
|