...

Source file src/cuelabs.dev/go/oci/ociregistry/ocimem/lister.go

Documentation: cuelabs.dev/go/oci/ociregistry/ocimem

     1  // Copyright 2023 CUE Labs AG
     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 ocimem
    16  
    17  import (
    18  	"context"
    19  	"sort"
    20  
    21  	"cuelabs.dev/go/oci/ociregistry"
    22  )
    23  
    24  func (r *Registry) Repositories(ctx context.Context, startAfter string) ociregistry.Seq[string] {
    25  	r.mu.Lock()
    26  	defer r.mu.Unlock()
    27  	return mapKeysIter(r.repos, stringLess, startAfter)
    28  }
    29  
    30  func (r *Registry) Tags(ctx context.Context, repoName string, startAfter string) ociregistry.Seq[string] {
    31  	r.mu.Lock()
    32  	defer r.mu.Unlock()
    33  	repo, err := r.repo(repoName)
    34  	if err != nil {
    35  		return ociregistry.ErrorIter[string](err)
    36  	}
    37  	return mapKeysIter(repo.tags, stringLess, startAfter)
    38  }
    39  
    40  func (r *Registry) Referrers(ctx context.Context, repoName string, digest ociregistry.Digest, artifactType string) ociregistry.Seq[ociregistry.Descriptor] {
    41  	r.mu.Lock()
    42  	defer r.mu.Unlock()
    43  	repo, err := r.repo(repoName)
    44  	if err != nil {
    45  		return ociregistry.ErrorIter[ociregistry.Descriptor](err)
    46  	}
    47  	var referrers []ociregistry.Descriptor
    48  	for _, b := range repo.manifests {
    49  		if b.subject != digest {
    50  			continue
    51  		}
    52  		// TODO filter by artifact type
    53  		referrers = append(referrers, b.descriptor())
    54  	}
    55  	sort.Slice(referrers, func(i, j int) bool {
    56  		return descriptorLess(referrers[i], referrers[j])
    57  	})
    58  	return ociregistry.SliceIter(referrers)
    59  }
    60  
    61  func mapKeysIter[K comparable, V any](m map[K]V, less func(K, K) bool, startAfter K) ociregistry.Seq[K] {
    62  	ks := make([]K, 0, len(m))
    63  	for k := range m {
    64  		if less(startAfter, k) {
    65  			ks = append(ks, k)
    66  		}
    67  	}
    68  	sort.Slice(ks, func(i, j int) bool {
    69  		return less(ks[i], ks[j])
    70  	})
    71  	return ociregistry.SliceIter(ks)
    72  }
    73  
    74  func stringLess(s1, s2 string) bool {
    75  	return s1 < s2
    76  }
    77  
    78  func descriptorLess(d1, d2 ociregistry.Descriptor) bool {
    79  	return d1.Digest < d2.Digest
    80  }
    81  

View as plain text