|
| 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 ContainerPersistence |
| 18 | +import Foundation |
| 19 | +import SystemPackage |
| 20 | +import TOML |
| 21 | + |
| 22 | +struct SystemProperties: Encodable { |
| 23 | + let build: BuildConfig |
| 24 | + let container: ContainerConfig |
| 25 | + let dns: DNSConfig |
| 26 | + let kernel: KernelConfig |
| 27 | + let machine: MachineConfig |
| 28 | + let network: NetworkConfig |
| 29 | + let registry: RegistryConfig |
| 30 | + let vminit: VminitConfig |
| 31 | + let plugin: [String: PluginPropertyValue]? |
| 32 | + |
| 33 | + init(config: ContainerSystemConfig, plugin: [String: PluginPropertyValue]) { |
| 34 | + self.build = config.build |
| 35 | + self.container = config.container |
| 36 | + self.dns = config.dns |
| 37 | + self.kernel = config.kernel |
| 38 | + self.machine = config.machine |
| 39 | + self.network = config.network |
| 40 | + self.registry = config.registry |
| 41 | + self.vminit = config.vminit |
| 42 | + self.plugin = plugin.isEmpty ? nil : plugin |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +enum PluginProperties { |
| 47 | + static func load(configurationFiles: [FilePath] = ConfigurationLoader.defaultConfigFiles()) throws -> [String: PluginPropertyValue] { |
| 48 | + var pluginProperties: [String: PluginPropertyValue] = [:] |
| 49 | + for path in configurationFiles.reversed() { |
| 50 | + guard FileManager.default.fileExists(atPath: path.string) else { |
| 51 | + continue |
| 52 | + } |
| 53 | + let data = try Data(contentsOf: URL(filePath: path.string)) |
| 54 | + guard let content = String(data: data, encoding: .utf8) else { |
| 55 | + continue |
| 56 | + } |
| 57 | + let document = try TOMLDecoder().decode(TOMLDocument.self, from: content) |
| 58 | + guard case .table(let pluginTable)? = document.values["plugin"] else { |
| 59 | + continue |
| 60 | + } |
| 61 | + pluginProperties = merge(pluginProperties, overlay: pluginTable) |
| 62 | + } |
| 63 | + return pluginProperties |
| 64 | + } |
| 65 | + |
| 66 | + private static func merge( |
| 67 | + _ base: [String: PluginPropertyValue], |
| 68 | + overlay: [String: PluginPropertyValue] |
| 69 | + ) -> [String: PluginPropertyValue] { |
| 70 | + base.merging(overlay) { old, new in |
| 71 | + if case .table(let oldTable) = old, case .table(let newTable) = new { |
| 72 | + return .table(merge(oldTable, overlay: newTable)) |
| 73 | + } |
| 74 | + return new |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +enum PluginPropertyValue: Codable, Equatable { |
| 80 | + case string(String) |
| 81 | + case integer(Int64) |
| 82 | + case float(Double) |
| 83 | + case boolean(Bool) |
| 84 | + case array([PluginPropertyValue]) |
| 85 | + case table([String: PluginPropertyValue]) |
| 86 | + |
| 87 | + init(from decoder: Decoder) throws { |
| 88 | + let container = try decoder.singleValueContainer() |
| 89 | + if let table = try? container.decode([String: PluginPropertyValue].self) { |
| 90 | + self = .table(table) |
| 91 | + } else if let array = try? container.decode([PluginPropertyValue].self) { |
| 92 | + self = .array(array) |
| 93 | + } else if let string = try? container.decode(String.self) { |
| 94 | + self = .string(string) |
| 95 | + } else if let integer = try? container.decode(Int64.self) { |
| 96 | + self = .integer(integer) |
| 97 | + } else if let float = try? container.decode(Double.self) { |
| 98 | + self = .float(float) |
| 99 | + } else if let boolean = try? container.decode(Bool.self) { |
| 100 | + self = .boolean(boolean) |
| 101 | + } else { |
| 102 | + throw DecodingError.typeMismatch( |
| 103 | + PluginPropertyValue.self, |
| 104 | + DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Unsupported plugin property value") |
| 105 | + ) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + func encode(to encoder: Encoder) throws { |
| 110 | + var container = encoder.singleValueContainer() |
| 111 | + switch self { |
| 112 | + case .string(let value): |
| 113 | + try container.encode(value) |
| 114 | + case .integer(let value): |
| 115 | + try container.encode(value) |
| 116 | + case .float(let value): |
| 117 | + try container.encode(value) |
| 118 | + case .boolean(let value): |
| 119 | + try container.encode(value) |
| 120 | + case .array(let value): |
| 121 | + try container.encode(value) |
| 122 | + case .table(let value): |
| 123 | + try container.encode(value) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +private struct TOMLDocument: Decodable { |
| 129 | + let values: [String: PluginPropertyValue] |
| 130 | + |
| 131 | + init(from decoder: Decoder) throws { |
| 132 | + let container = try decoder.container(keyedBy: DynamicCodingKey.self) |
| 133 | + var values: [String: PluginPropertyValue] = [:] |
| 134 | + for key in container.allKeys { |
| 135 | + values[key.stringValue] = try container.decode(PluginPropertyValue.self, forKey: key) |
| 136 | + } |
| 137 | + self.values = values |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +private struct DynamicCodingKey: CodingKey { |
| 142 | + let stringValue: String |
| 143 | + let intValue: Int? |
| 144 | + |
| 145 | + init?(stringValue: String) { |
| 146 | + self.stringValue = stringValue |
| 147 | + self.intValue = nil |
| 148 | + } |
| 149 | + |
| 150 | + init?(intValue: Int) { |
| 151 | + self.stringValue = "\(intValue)" |
| 152 | + self.intValue = intValue |
| 153 | + } |
| 154 | +} |
0 commit comments