...

Source file src/github.com/google/go-containerregistry/pkg/v1/match/match.go

Documentation: github.com/google/go-containerregistry/pkg/v1/match

     1  // Copyright 2020 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package match provides functionality for conveniently matching a v1.Descriptor.
    16  package match
    17  
    18  import (
    19  	v1 "github.com/google/go-containerregistry/pkg/v1"
    20  	imagespec "github.com/opencontainers/image-spec/specs-go/v1"
    21  )
    22  
    23  // Matcher function that is given a v1.Descriptor, and returns whether or
    24  // not it matches a given rule. Can match on anything it wants in the Descriptor.
    25  type Matcher func(desc v1.Descriptor) bool
    26  
    27  // Name returns a match.Matcher that matches based on the value of the
    28  //
    29  //	"org.opencontainers.image.ref.name" annotation:
    30  //
    31  // github.com/opencontainers/image-spec/blob/v1.0.1/annotations.md#pre-defined-annotation-keys
    32  func Name(name string) Matcher {
    33  	return Annotation(imagespec.AnnotationRefName, name)
    34  }
    35  
    36  // Annotation returns a match.Matcher that matches based on the provided annotation.
    37  func Annotation(key, value string) Matcher {
    38  	return func(desc v1.Descriptor) bool {
    39  		if desc.Annotations == nil {
    40  			return false
    41  		}
    42  		if aValue, ok := desc.Annotations[key]; ok && aValue == value {
    43  			return true
    44  		}
    45  		return false
    46  	}
    47  }
    48  
    49  // Platforms returns a match.Matcher that matches on any one of the provided platforms.
    50  // Ignores any descriptors that do not have a platform.
    51  func Platforms(platforms ...v1.Platform) Matcher {
    52  	return func(desc v1.Descriptor) bool {
    53  		if desc.Platform == nil {
    54  			return false
    55  		}
    56  		for _, platform := range platforms {
    57  			if desc.Platform.Equals(platform) {
    58  				return true
    59  			}
    60  		}
    61  		return false
    62  	}
    63  }
    64  
    65  // MediaTypes returns a match.Matcher that matches at least one of the provided media types.
    66  func MediaTypes(mediaTypes ...string) Matcher {
    67  	mts := map[string]bool{}
    68  	for _, media := range mediaTypes {
    69  		mts[media] = true
    70  	}
    71  	return func(desc v1.Descriptor) bool {
    72  		if desc.MediaType == "" {
    73  			return false
    74  		}
    75  		if _, ok := mts[string(desc.MediaType)]; ok {
    76  			return true
    77  		}
    78  		return false
    79  	}
    80  }
    81  
    82  // Digests returns a match.Matcher that matches at least one of the provided Digests
    83  func Digests(digests ...v1.Hash) Matcher {
    84  	digs := map[v1.Hash]bool{}
    85  	for _, digest := range digests {
    86  		digs[digest] = true
    87  	}
    88  	return func(desc v1.Descriptor) bool {
    89  		_, ok := digs[desc.Digest]
    90  		return ok
    91  	}
    92  }
    93  

View as plain text