mirror of
https://github.com/apple/container.git
synced 2026-07-26 19:31:33 +00:00
## Type of Change - [x] New feature ## Motivation and Context Related to https://github.com/apple/container/issues/1404. This PR adds the initial work to have volume resources conform to ManagedResource, in alignment with other resources such as networks (see [here](https://github.com/apple/container/pull/1421)). Further work is necessary to move the use of `VolumeResource` down to the APIServer (in the VolumesService) and in the volume client. Volumes do not currently have any plugin or runtime state, so that information is not included in the `VolumeResource`, but could be added later if needed. ## Testing - [x] Tested locally Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
106 lines
4.1 KiB
Swift
106 lines
4.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
// Copyright © 2026 Apple Inc. and the container project authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Foundation
|
|
import TOML
|
|
import Yams
|
|
|
|
/// Options for JSON rendering, wrapping the knobs on `JSONEncoder`.
|
|
public struct JSONOptions: Sendable {
|
|
public var outputFormatting: JSONEncoder.OutputFormatting = []
|
|
public var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate
|
|
|
|
public static let compact = JSONOptions()
|
|
public static let prettySorted = JSONOptions(outputFormatting: [.prettyPrinted, .sortedKeys])
|
|
|
|
public init(
|
|
outputFormatting: JSONEncoder.OutputFormatting = [],
|
|
dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate
|
|
) {
|
|
self.outputFormatting = outputFormatting
|
|
self.dateEncodingStrategy = dateEncodingStrategy
|
|
}
|
|
}
|
|
|
|
/// Shared rendering helpers for CLI output.
|
|
///
|
|
/// All list commands route their output through these methods. JSON rendering
|
|
/// is separate from table/quiet rendering because the JSON model often differs
|
|
/// from the display model.
|
|
public enum Output {
|
|
/// Renders an `Encodable` value as a JSON string.
|
|
public static func renderJSON<T: Encodable>(_ value: T, options: JSONOptions = .compact) throws -> String {
|
|
let encoder = JSONEncoder()
|
|
encoder.outputFormatting = options.outputFormatting
|
|
encoder.dateEncodingStrategy = options.dateEncodingStrategy
|
|
let data = try encoder.encode(value)
|
|
return String(decoding: data, as: UTF8.self)
|
|
}
|
|
|
|
public static func renderYAML<T: Encodable>(_ value: T) throws -> String {
|
|
let encoder = YAMLEncoder()
|
|
let data = try encoder.encode(value)
|
|
return data
|
|
}
|
|
|
|
/// Renders an `Encodable` value as a TOML string.
|
|
public static func renderTOML<T: Encodable>(_ value: T) throws -> String {
|
|
let encoder = TOMLEncoder()
|
|
encoder.outputFormatting = .sortedKeys
|
|
return try encoder.encodeToString(value)
|
|
}
|
|
|
|
/// Renders a list of displayable items as a table (with header) or quiet-mode identifiers.
|
|
public static func renderList<T: ListDisplayable>(_ items: [T], quiet: Bool) -> String {
|
|
if quiet {
|
|
return items.map(\.quietValue).joined(separator: "\n")
|
|
}
|
|
return renderTable(items)
|
|
}
|
|
|
|
/// Renders a list of displayable items as a column-aligned table with a header row.
|
|
public static func renderTable<T: ListDisplayable>(_ items: [T]) -> String {
|
|
var rows: [[String]] = [T.tableHeader]
|
|
for item in items {
|
|
rows.append(item.tableRow)
|
|
}
|
|
return TableOutput(rows: rows).format()
|
|
}
|
|
|
|
/// Renders list output in the requested format.
|
|
///
|
|
/// The JSON and display models may be the same type (e.g., `PrintableContainer`)
|
|
/// or different types.
|
|
public static func render<J: Encodable, D: ListDisplayable>(
|
|
json: J, display: [D], format: ListFormat, quiet: Bool
|
|
) throws {
|
|
switch format {
|
|
case .json: try emit(renderJSON(json))
|
|
case .yaml: try emit(renderYAML(json))
|
|
case .table: emit(renderList(display, quiet: quiet))
|
|
case .toml: try emit(renderTOML(json))
|
|
}
|
|
}
|
|
|
|
/// Writes rendered output to stdout. No-ops on empty strings to avoid blank lines
|
|
/// (e.g., `container list -q` with zero results should produce no output, not a newline).
|
|
public static func emit(_ output: String) {
|
|
if !output.isEmpty {
|
|
print(output)
|
|
}
|
|
}
|
|
}
|