@@ -80,14 +80,13 @@ defmodule OCI.Registry do
8080 end
8181
8282 @ spec validate_repository_name ( registry :: t ( ) , repo :: repo_t ( ) ) ::
83- { :ok , repo :: repo_t ( ) } | { :error , :NAME_INVALID , String . t ( ) }
83+ { :ok , repo :: repo_t ( ) } | { :error , :NAME_INVALID , map ( ) }
8484
8585 def validate_repository_name ( registry , repo ) do
8686 if Regex . match? ( registry . repo_name_pattern , repo ) do
8787 { :ok , repo }
8888 else
89- { :error , :NAME_INVALID ,
90- "Invalid repo name: #{ repo } , must match pattern: #{ inspect ( registry . repo_name_pattern ) } ." }
89+ { :error , :NAME_INVALID , % { repo: repo , pattern: inspect ( registry . repo_name_pattern ) } }
9190 end
9291 end
9392
@@ -149,12 +148,11 @@ defmodule OCI.Registry do
149148 - `{:error, reason}` if the upload fails
150149 """
151150 def upload_blob_chunk ( % { storage: storage } , repo , uuid , chunk , maybe_chunk_range , ctx ) do
152- reg = adapter ( storage )
153-
154- with true <- reg . upload_exists? ( storage , repo , uuid , ctx ) ,
155- { :ok , size } <- reg . get_blob_upload_offset ( storage , repo , uuid , ctx ) ,
151+ with :ok <- ensure_upload_exists ( storage , repo , uuid , ctx ) ,
152+ { :ok , size } <- adapter ( storage ) . get_blob_upload_offset ( storage , repo , uuid , ctx ) ,
156153 :ok <- verify_upload_order ( size , maybe_chunk_range ) ,
157- { :ok , range } <- reg . upload_blob_chunk ( storage , repo , uuid , chunk , maybe_chunk_range , ctx ) do
154+ { :ok , range } <-
155+ adapter ( storage ) . upload_blob_chunk ( storage , repo , uuid , chunk , maybe_chunk_range , ctx ) do
158156 { :ok , blobs_uploads_path ( repo , uuid ) , range }
159157 end
160158 end
@@ -185,22 +183,26 @@ defmodule OCI.Registry do
185183 do: { :error , :DIGEST_INVALID , % { repo: repo , uuid: uuid } }
186184
187185 def complete_blob_upload ( % { storage: storage } , repo , uuid , digest , ctx ) do
188- with true <- adapter ( storage ) . upload_exists? ( storage , repo , uuid , ctx ) ,
186+ with :ok <- ensure_upload_exists ( storage , repo , uuid , ctx ) ,
189187 :ok <- adapter ( storage ) . complete_blob_upload ( storage , repo , uuid , digest , ctx ) do
190188 { :ok , blobs_digest_path ( repo , digest ) }
191189 end
192190 end
193191
194192 def cancel_blob_upload ( % { storage: storage } , repo , uuid , ctx ) do
195- adapter ( storage ) . cancel_blob_upload ( storage , repo , uuid , ctx )
193+ with :ok <- ensure_upload_exists ( storage , repo , uuid , ctx ) do
194+ adapter ( storage ) . cancel_blob_upload ( storage , repo , uuid , ctx )
195+ end
196196 end
197197
198198 def blob_exists? ( % { storage: storage } , repo , digest , ctx ) do
199199 adapter ( storage ) . blob_exists? ( storage , repo , digest , ctx )
200200 end
201201
202202 def get_blob ( % { storage: storage } , repo , digest , ctx ) do
203- adapter ( storage ) . get_blob ( storage , repo , digest , ctx )
203+ with :ok <- ensure_repo_exists ( storage , repo , ctx ) do
204+ adapter ( storage ) . get_blob ( storage , repo , digest , ctx )
205+ end
204206 end
205207
206208 def delete_blob ( % { enable_blob_deletion: false } , repo , digest , _ctx ) ,
@@ -298,18 +300,18 @@ defmodule OCI.Registry do
298300 def referenced_blobs ( _manifest ) , do: [ ]
299301
300302 def get_manifest ( % { storage: storage } , repo , reference , ctx ) do
301- adapter ( storage ) . get_manifest ( storage , repo , reference , ctx )
303+ with :ok <- ensure_repo_exists ( storage , repo , ctx ) do
304+ adapter ( storage ) . get_manifest ( storage , repo , reference , ctx )
305+ end
302306 end
303307
304308 def manifest_exists? ( % { storage: storage } , repo , reference , ctx ) do
305309 adapter ( storage ) . manifest_exists? ( storage , repo , reference , ctx )
306310 end
307311
308312 def list_tags ( % { storage: storage } , repo , pagination , ctx ) do
309- if repo_exists? ( storage , repo , ctx ) do
313+ with :ok <- ensure_repo_exists ( storage , repo , ctx ) do
310314 adapter ( storage ) . list_tags ( storage , repo , pagination , ctx )
311- else
312- { :error , :NAME_UNKNOWN , % { repo: repo } }
313315 end
314316 end
315317
@@ -366,17 +368,14 @@ defmodule OCI.Registry do
366368 Returns {:ok, location} on success, {:error, :BLOB_UNKNOWN} if the source blob doesn't exist.
367369 """
368370 def mount_blob ( % __MODULE__ { storage: storage } = registry , repo , digest , from_repo , ctx ) do
369- if repo_exists? ( storage , from_repo , ctx ) do
371+ with :ok <- ensure_repo_exists ( storage , from_repo , ctx ) do
370372 if blob_exists? ( registry , from_repo , digest , ctx ) do
371- # credo:disable-for-next-line
372373 with :ok <- adapter ( storage ) . mount_blob ( storage , repo , digest , from_repo , ctx ) do
373374 { :ok , blobs_digest_path ( repo , digest ) }
374375 end
375376 else
376377 initiate_blob_upload ( registry , repo , ctx )
377378 end
378- else
379- { :error , :NAME_UNKNOWN , % { repo: repo } }
380379 end
381380 end
382381
@@ -431,6 +430,25 @@ defmodule OCI.Registry do
431430 "/#{ api_version ( ) } /#{ repo } /blobs/uploads/#{ uuid } "
432431 end
433432
433+ # Precondition checks — used by registry functions to validate state before
434+ # delegating to storage adapters.
435+
436+ defp ensure_repo_exists ( storage , repo , ctx ) do
437+ if adapter ( storage ) . repo_exists? ( storage , repo , ctx ) do
438+ :ok
439+ else
440+ { :error , :NAME_UNKNOWN , % { repo: repo } }
441+ end
442+ end
443+
444+ defp ensure_upload_exists ( storage , repo , uuid , ctx ) do
445+ if adapter ( storage ) . upload_exists? ( storage , repo , uuid , ctx ) do
446+ :ok
447+ else
448+ { :error , :BLOB_UPLOAD_UNKNOWN , % { repo: repo , uuid: uuid } }
449+ end
450+ end
451+
434452 @ doc """
435453 Calculates the range of a chunk of data.
436454 ## Examples
0 commit comments