Skip to content

Commit f38a078

Browse files
committed
ContainerRegistry: Define ImageSource protocol
1 parent 6e57637 commit f38a078

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftContainerPlugin open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Foundation
16+
17+
public protocol ImageSource {
18+
var decoder: JSONDecoder { get }
19+
20+
/// Fetches an unstructured blob of data from the registry.
21+
///
22+
/// - Parameters:
23+
/// - repository: Name of the repository containing the blob.
24+
/// - digest: Digest of the blob.
25+
/// - Returns: The downloaded data.
26+
/// - Throws: If the blob download fails.
27+
func getBlob(repository: ImageReference.Repository, digest: ImageReference.Digest) async throws -> Data
28+
29+
func getManifest(
30+
repository: ImageReference.Repository,
31+
reference: any ImageReference.Reference
32+
) async throws -> (ImageManifest, ContentDescriptor)
33+
34+
func getIndex(
35+
repository: ImageReference.Repository,
36+
reference: any ImageReference.Reference
37+
) async throws -> ImageIndex
38+
}

Sources/ContainerRegistry/RegistryClient+ImageConfiguration.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
extension RegistryClient {
15+
import class Foundation.JSONDecoder
16+
17+
extension ImageSource {
1618
/// Get an image configuration record from the registry.
1719
/// - Parameters:
1820
/// - image: Reference to the image containing the record.

Sources/ContainerRegistry/RegistryClient.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public struct RegistryClient {
5959
var authChallenge: AuthChallenge
6060

6161
var encoder: JSONEncoder
62-
var decoder: JSONDecoder
62+
public var decoder: JSONDecoder
6363

6464
/// Creates a new RegistryClient
6565
/// - Parameters:
@@ -375,5 +375,5 @@ extension RegistryClient {
375375
/// Make decoded registry errors throwable
376376
extension DistributionErrors: Error {}
377377

378-
/// Images can be uploaded to a registry using a RegistryClient instance
379-
extension RegistryClient: ImageDestination {}
378+
/// RegistryClient can both fetch and upload images
379+
extension RegistryClient: ImageSource, ImageDestination {}

Sources/containertool/Extensions/RegistryClient+CopyBlobs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import ContainerRegistry
1616

17-
extension RegistryClient {
17+
extension ImageSource {
1818
/// Copies a blob from another registry to this one.
1919
/// - Parameters:
2020
/// - digest: The digest of the blob to copy.

Sources/containertool/Extensions/RegistryClient+Layers.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
import struct Foundation.Data
1616
import ContainerRegistry
1717

18-
extension RegistryClient {
19-
func getImageManifest(forImage image: ImageReference, architecture: String) async throws -> (
20-
ImageManifest, ContentDescriptor
21-
) {
18+
extension ImageSource {
19+
public func getImageManifest(
20+
forImage image: ImageReference,
21+
architecture: String
22+
) async throws -> (ImageManifest, ContentDescriptor) {
2223
do {
2324
// Try to retrieve a manifest. If the object with this reference is actually an index, the content-type will not match and
2425
// an error will be thrown.

Sources/containertool/containertool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
233233
}
234234
}
235235

236-
func publishContainerImage<Destination: ImageDestination>(
236+
func publishContainerImage<Source: ImageSource, Destination: ImageDestination>(
237237
baseImage: ImageReference,
238238
destinationImage: ImageReference,
239-
source: RegistryClient?,
239+
source: Source?,
240240
destination: Destination,
241241
architecture: String,
242242
os: String,

0 commit comments

Comments
 (0)