cuelabs.dev/go/oci/ociregistry@v0.0.0-20240906074133-82eb438dd565/func.go (about) 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 ociregistry 16 17 import ( 18 "context" 19 "fmt" 20 "io" 21 ) 22 23 var _ Interface = (*Funcs)(nil) 24 25 // Funcs implements Interface by calling its member functions: there's one field 26 // for every corresponding method of [Interface]. 27 // 28 // When a function is nil, the corresponding method will return 29 // an [ErrUnsupported] error. For nil functions that return an iterator, 30 // the corresponding method will return an iterator that returns no items and 31 // returns ErrUnsupported from its Err method. 32 // 33 // If Funcs is nil itself, all methods will behave as if the corresponding field was nil, 34 // so (*ociregistry.Funcs)(nil) is a useful placeholder to implement Interface. 35 // 36 // If you're writing your own implementation of Funcs, you'll need to embed a *Funcs 37 // value to get an implementation of the private method. This means that it will 38 // be possible to add members to Interface in the future without breaking compatibility. 39 type Funcs struct { 40 NewError func(ctx context.Context, methodName, repo string) error 41 42 GetBlob_ func(ctx context.Context, repo string, digest Digest) (BlobReader, error) 43 GetBlobRange_ func(ctx context.Context, repo string, digest Digest, offset0, offset1 int64) (BlobReader, error) 44 GetManifest_ func(ctx context.Context, repo string, digest Digest) (BlobReader, error) 45 GetTag_ func(ctx context.Context, repo string, tagName string) (BlobReader, error) 46 ResolveBlob_ func(ctx context.Context, repo string, digest Digest) (Descriptor, error) 47 ResolveManifest_ func(ctx context.Context, repo string, digest Digest) (Descriptor, error) 48 ResolveTag_ func(ctx context.Context, repo string, tagName string) (Descriptor, error) 49 PushBlob_ func(ctx context.Context, repo string, desc Descriptor, r io.Reader) (Descriptor, error) 50 PushBlobChunked_ func(ctx context.Context, repo string, chunkSize int) (BlobWriter, error) 51 PushBlobChunkedResume_ func(ctx context.Context, repo, id string, offset int64, chunkSize int) (BlobWriter, error) 52 MountBlob_ func(ctx context.Context, fromRepo, toRepo string, digest Digest) (Descriptor, error) 53 PushManifest_ func(ctx context.Context, repo string, tag string, contents []byte, mediaType string) (Descriptor, error) 54 DeleteBlob_ func(ctx context.Context, repo string, digest Digest) error 55 DeleteManifest_ func(ctx context.Context, repo string, digest Digest) error 56 DeleteTag_ func(ctx context.Context, repo string, name string) error 57 Repositories_ func(ctx context.Context, startAfter string) Seq[string] 58 Tags_ func(ctx context.Context, repo string, startAfter string) Seq[string] 59 Referrers_ func(ctx context.Context, repo string, digest Digest, artifactType string) Seq[Descriptor] 60 } 61 62 // This blesses Funcs as the canonical Interface implementation. 63 func (*Funcs) private() {} 64 65 func (f *Funcs) newError(ctx context.Context, methodName, repo string) error { 66 if f != nil && f.NewError != nil { 67 return f.NewError(ctx, methodName, repo) 68 } 69 return fmt.Errorf("%s: %w", methodName, ErrUnsupported) 70 } 71 72 func (f *Funcs) GetBlob(ctx context.Context, repo string, digest Digest) (BlobReader, error) { 73 if f != nil && f.GetBlob_ != nil { 74 return f.GetBlob_(ctx, repo, digest) 75 } 76 return nil, f.newError(ctx, "GetBlob", repo) 77 } 78 79 func (f *Funcs) GetBlobRange(ctx context.Context, repo string, digest Digest, offset0, offset1 int64) (BlobReader, error) { 80 if f != nil && f.GetBlobRange_ != nil { 81 return f.GetBlobRange_(ctx, repo, digest, offset0, offset1) 82 } 83 return nil, f.newError(ctx, "GetBlobRange", repo) 84 } 85 86 func (f *Funcs) GetManifest(ctx context.Context, repo string, digest Digest) (BlobReader, error) { 87 if f != nil && f.GetManifest_ != nil { 88 return f.GetManifest_(ctx, repo, digest) 89 } 90 return nil, f.newError(ctx, "GetManifest", repo) 91 } 92 93 func (f *Funcs) GetTag(ctx context.Context, repo string, tagName string) (BlobReader, error) { 94 if f != nil && f.GetTag_ != nil { 95 return f.GetTag_(ctx, repo, tagName) 96 } 97 return nil, f.newError(ctx, "GetTag", repo) 98 } 99 100 func (f *Funcs) ResolveBlob(ctx context.Context, repo string, digest Digest) (Descriptor, error) { 101 if f != nil && f.ResolveBlob_ != nil { 102 return f.ResolveBlob_(ctx, repo, digest) 103 } 104 return Descriptor{}, f.newError(ctx, "ResolveBlob", repo) 105 } 106 107 func (f *Funcs) ResolveManifest(ctx context.Context, repo string, digest Digest) (Descriptor, error) { 108 if f != nil && f.ResolveManifest_ != nil { 109 return f.ResolveManifest_(ctx, repo, digest) 110 } 111 return Descriptor{}, f.newError(ctx, "ResolveManifest", repo) 112 } 113 114 func (f *Funcs) ResolveTag(ctx context.Context, repo string, tagName string) (Descriptor, error) { 115 if f != nil && f.ResolveTag_ != nil { 116 return f.ResolveTag_(ctx, repo, tagName) 117 } 118 return Descriptor{}, f.newError(ctx, "ResolveTag", repo) 119 } 120 121 func (f *Funcs) PushBlob(ctx context.Context, repo string, desc Descriptor, r io.Reader) (Descriptor, error) { 122 if f != nil && f.PushBlob_ != nil { 123 return f.PushBlob_(ctx, repo, desc, r) 124 } 125 return Descriptor{}, f.newError(ctx, "PushBlob", repo) 126 } 127 128 func (f *Funcs) PushBlobChunked(ctx context.Context, repo string, chunkSize int) (BlobWriter, error) { 129 if f != nil && f.PushBlobChunked_ != nil { 130 return f.PushBlobChunked_(ctx, repo, chunkSize) 131 } 132 return nil, f.newError(ctx, "PushBlobChunked", repo) 133 } 134 135 func (f *Funcs) PushBlobChunkedResume(ctx context.Context, repo, id string, offset int64, chunkSize int) (BlobWriter, error) { 136 if f != nil && f.PushBlobChunked_ != nil { 137 return f.PushBlobChunkedResume_(ctx, repo, id, offset, chunkSize) 138 } 139 return nil, f.newError(ctx, "PushBlobChunked", repo) 140 } 141 142 func (f *Funcs) MountBlob(ctx context.Context, fromRepo, toRepo string, digest Digest) (Descriptor, error) { 143 if f != nil && f.MountBlob_ != nil { 144 return f.MountBlob_(ctx, fromRepo, toRepo, digest) 145 } 146 return Descriptor{}, f.newError(ctx, "MountBlob", toRepo) 147 } 148 149 func (f *Funcs) PushManifest(ctx context.Context, repo string, tag string, contents []byte, mediaType string) (Descriptor, error) { 150 if f != nil && f.PushManifest_ != nil { 151 return f.PushManifest_(ctx, repo, tag, contents, mediaType) 152 } 153 return Descriptor{}, f.newError(ctx, "PushManifest", repo) 154 } 155 156 func (f *Funcs) DeleteBlob(ctx context.Context, repo string, digest Digest) error { 157 if f != nil && f.DeleteBlob_ != nil { 158 return f.DeleteBlob_(ctx, repo, digest) 159 } 160 return f.newError(ctx, "DeleteBlob", repo) 161 } 162 163 func (f *Funcs) DeleteManifest(ctx context.Context, repo string, digest Digest) error { 164 if f != nil && f.DeleteManifest_ != nil { 165 return f.DeleteManifest_(ctx, repo, digest) 166 } 167 return f.newError(ctx, "DeleteManifest", repo) 168 } 169 170 func (f *Funcs) DeleteTag(ctx context.Context, repo string, name string) error { 171 if f != nil && f.DeleteTag_ != nil { 172 return f.DeleteTag_(ctx, repo, name) 173 } 174 return f.newError(ctx, "DeleteTag", repo) 175 } 176 177 func (f *Funcs) Repositories(ctx context.Context, startAfter string) Seq[string] { 178 if f != nil && f.Repositories_ != nil { 179 return f.Repositories_(ctx, startAfter) 180 } 181 return ErrorSeq[string](f.newError(ctx, "Repositories", "")) 182 } 183 184 func (f *Funcs) Tags(ctx context.Context, repo string, startAfter string) Seq[string] { 185 if f != nil && f.Tags_ != nil { 186 return f.Tags_(ctx, repo, startAfter) 187 } 188 return ErrorSeq[string](f.newError(ctx, "Tags", repo)) 189 } 190 191 func (f *Funcs) Referrers(ctx context.Context, repo string, digest Digest, artifactType string) Seq[Descriptor] { 192 if f != nil && f.Referrers_ != nil { 193 return f.Referrers_(ctx, repo, digest, artifactType) 194 } 195 return ErrorSeq[Descriptor](f.newError(ctx, "Referrers", repo)) 196 }