Skip to content

Commit c42108d

Browse files
authored
[image-inspect]: stdout/stderr and logging refactor (apple#1044)
- Relates to apple#642. - Each image carries it own error message (allows for different error types per image) - Stdout/stderr support - Use of the new `StderrLogHandler`
1 parent a18df81 commit c42108d

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

Sources/ContainerCommands/Image/ImageInspect.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
import ArgumentParser
1818
import ContainerAPIClient
19+
import ContainerLog
1920
import ContainerizationError
2021
import Foundation
22+
import Logging
2123
import SwiftProtobuf
2224

2325
extension Application {
@@ -34,21 +36,39 @@ extension Application {
3436

3537
public init() {}
3638

39+
struct InspectError: Error {
40+
let succeeded: [String]
41+
let failed: [(String, Error)]
42+
}
43+
3744
public func run() async throws {
3845
var printable = [any Codable]()
46+
var succeededImages: [String] = []
47+
var allErrors: [(String, Error)] = []
48+
3949
let result = try await ClientImage.get(names: images)
40-
let notFound = result.error
50+
4151
for image in result.images {
42-
guard !Utility.isInfraImage(name: image.reference) else {
43-
continue
44-
}
52+
guard !Utility.isInfraImage(name: image.reference) else { continue }
4553
printable.append(try await image.details())
54+
succeededImages.append(image.reference)
4655
}
47-
if printable.count > 0 {
56+
57+
for missing in result.error {
58+
allErrors.append((missing, ContainerizationError(.notFound, message: "Image not found")))
59+
}
60+
61+
if !printable.isEmpty {
4862
print(try printable.jsonArray())
4963
}
50-
if notFound.count > 0 {
51-
throw ContainerizationError(.notFound, message: "images: \(notFound.joined(separator: "\n"))")
64+
65+
if !allErrors.isEmpty {
66+
let logger = Logger(label: "ImageInspect", factory: { _ in StderrLogHandler() })
67+
for (name, error) in allErrors {
68+
logger.error("\(name): \(error.localizedDescription)")
69+
}
70+
71+
throw InspectError(succeeded: succeededImages, failed: allErrors)
5272
}
5373
}
5474
}

0 commit comments

Comments
 (0)