mirror of
https://github.com/apple/container.git
synced 2026-07-17 15:06:59 +00:00
e3c49803a0
- Discussion topic #1336. - This change migrates away from using `UserDefaults`, instead providing a TOML configuration mechanism for user configurable settings. All existing system property settings keys are supported in the new configuration file. However, users will have to migrate any settings they have configured in the `UserDefaults` into TOML for these settings to take effect. - Breaking changes: * `container system property get` is removed in favor of users directly utilizing `container system property list --format toml | jq<>`. * `container system property set` is removed since the TOML configuration is effectively immutable during the lifetime of the `container` daemon. Uses can edit the TOML they have in their home directory, however no changes will take effect until the daemon is restarted via `container system stop && container system start` * `container system property list --format table` is removed as generating tabular format is non-trivial and the new TOML format is intended to be human readable
106 lines
4.2 KiB
Swift
106 lines
4.2 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 (e.g., `Volume` for JSON vs `PrintableVolume` for table).
|
|
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 (e.g., `Volume` for JSON and `PrintableVolume` for table).
|
|
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)
|
|
}
|
|
}
|
|
}
|