Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions Sources/ContainerCommands/Image/ImageInspect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

import ArgumentParser
import ContainerAPIClient
import ContainerLog
import ContainerizationError
import Foundation
import Logging
import SwiftProtobuf

extension Application {
public struct ImageInspect: AsyncParsableCommand {
public init() {}

public static let configuration = CommandConfiguration(
commandName: "inspect",
abstract: "Display information about one or more images")
Expand All @@ -32,23 +36,39 @@ extension Application {
@Argument(help: "Images to inspect")
var images: [String]

public init() {}
struct InspectError: Error {
let succeeded: [String]
let failed: [(String, Error)]
}

public func run() async throws {
var printable = [any Codable]()
var succeededImages: [String] = []
var allErrors: [(String, Error)] = []

let result = try await ClientImage.get(names: images)
let notFound = result.error

for image in result.images {
guard !Utility.isInfraImage(name: image.reference) else {
continue
}
guard !Utility.isInfraImage(name: image.reference) else { continue }
printable.append(try await image.details())
succeededImages.append(image.reference)
}
if printable.count > 0 {

for missing in result.error {
allErrors.append((missing, ContainerizationError(.notFound, message: "Image not found")))
}

if !printable.isEmpty {
print(try printable.jsonArray())
}
if notFound.count > 0 {
throw ContainerizationError(.notFound, message: "images: \(notFound.joined(separator: "\n"))")

if !allErrors.isEmpty {
let logger = Logger(label: "ImageInspect", factory: { _ in StderrLogHandler() })
for (name, error) in allErrors {
logger.error("\(name): \(error.localizedDescription)")
}

throw InspectError(succeeded: succeededImages, failed: allErrors)
}
}
}
Expand Down