|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2026 Apple Inc. and the container project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import ArgumentParser |
| 18 | +import ContainerAPIClient |
| 19 | +import ContainerizationOCI |
| 20 | +import ContainerizationOS |
| 21 | +import Foundation |
| 22 | + |
| 23 | +extension Application { |
| 24 | + public struct RegistryList: AsyncLoggableCommand { |
| 25 | + @OptionGroup |
| 26 | + public var logOptions: Flags.Logging |
| 27 | + |
| 28 | + @Option(name: .long, help: "Format of the output") |
| 29 | + var format: ListFormat = .table |
| 30 | + |
| 31 | + @Flag(name: .shortAndLong, help: "Only output the registry name") |
| 32 | + var quiet = false |
| 33 | + |
| 34 | + public init() {} |
| 35 | + public static let configuration = CommandConfiguration( |
| 36 | + commandName: "list", |
| 37 | + abstract: "List image registry logins", |
| 38 | + aliases: ["ls"]) |
| 39 | + |
| 40 | + public func run() async throws { |
| 41 | + let keychain = KeychainHelper(securityDomain: Constants.keychainID) |
| 42 | + let registries = try keychain.list() |
| 43 | + try printRegistries(registries: registries, format: format) |
| 44 | + } |
| 45 | + |
| 46 | + private func createHeader() -> [[String]] { |
| 47 | + [["HOSTNAME", "USERNAME", "MODIFIED", "CREATED"]] |
| 48 | + } |
| 49 | + |
| 50 | + private func printRegistries(registries: [RegistryInfo], format: ListFormat) throws { |
| 51 | + if format == .json { |
| 52 | + let printables = registries.map { |
| 53 | + PrintableRegistry($0) |
| 54 | + } |
| 55 | + let data = try JSONEncoder().encode(printables) |
| 56 | + print(String(decoding: data, as: UTF8.self)) |
| 57 | + |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + if self.quiet { |
| 62 | + registries.forEach { |
| 63 | + print($0.hostname) |
| 64 | + } |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + var rows = createHeader() |
| 69 | + for registry in registries { |
| 70 | + rows.append(registry.asRow) |
| 71 | + } |
| 72 | + |
| 73 | + let formatter = TableOutput(rows: rows) |
| 74 | + print(formatter.format()) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +extension RegistryInfo { |
| 79 | + fileprivate var asRow: [String] { |
| 80 | + [ |
| 81 | + self.hostname, |
| 82 | + self.username, |
| 83 | + self.modifiedDate.ISO8601Format(), |
| 84 | + self.createdDate.ISO8601Format(), |
| 85 | + ] |
| 86 | + } |
| 87 | +} |
| 88 | +struct PrintableRegistry: Codable { |
| 89 | + let hostname: String |
| 90 | + let username: String |
| 91 | + let modifiedDate: Date |
| 92 | + let createdDate: Date |
| 93 | + |
| 94 | + init(_ registry: RegistryInfo) { |
| 95 | + self.hostname = registry.hostname |
| 96 | + self.username = registry.username |
| 97 | + self.modifiedDate = registry.modifiedDate |
| 98 | + self.createdDate = registry.createdDate |
| 99 | + } |
| 100 | +} |
0 commit comments