Add a new Match method to the reference package

The Match method allows to see if a reference matches a specified
patterns.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2016-11-09 22:04:52 +01:00
parent fbe6e8d212
commit 353e3a4c9d
No known key found for this signature in database
GPG key ID: 083CC6FD6EB699A3
2 changed files with 89 additions and 0 deletions

View file

@ -24,6 +24,7 @@ package reference
import (
"errors"
"fmt"
"path"
"strings"
"github.com/docker/distribution/digest"
@ -236,6 +237,16 @@ func WithDigest(name Named, digest digest.Digest) (Canonical, error) {
}, nil
}
// Match reports whether ref matches the specified pattern.
// See https://godoc.org/path#Match for supported patterns.
func Match(pattern string, ref Reference) (bool, error) {
matched, err := path.Match(pattern, ref.String())
if namedRef, isNamed := ref.(Named); isNamed && !matched {
matched, _ = path.Match(pattern, namedRef.Name())
}
return matched, err
}
func getBestReferenceType(ref reference) Reference {
if ref.name == "" {
// Allow digest only references