|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Apple Container runtime feature detection and gating |
| 4 | +/// Provides compatibility layer for 0.10.0 and 0.11.0 runtimes |
| 5 | +public enum RuntimeFeatureGate { |
| 6 | + |
| 7 | + // MARK: - Feature Flags |
| 8 | + |
| 9 | + /// Features available in 0.11.0+ only |
| 10 | + public enum Feature: String, CaseIterable { |
| 11 | + case scheme = "--scheme flag for registry protocol control" |
| 12 | + case buildSecrets = "--secret flag for build-time secrets" |
| 13 | + case containerDefaultPlatform = "CONTAINER_DEFAULT_PLATFORM env var" |
| 14 | + case containerPrune = "Container prune on startup" |
| 15 | + |
| 16 | + var minimumVersion: String { |
| 17 | + switch self { |
| 18 | + case .scheme, .buildSecrets, .containerDefaultPlatform, .containerPrune: |
| 19 | + return "0.11.0" |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + // MARK: - Version Detection |
| 25 | + |
| 26 | + /// Cached runtime version (computed once) |
| 27 | + private static let cachedVersion: String? = { |
| 28 | + let process = Process() |
| 29 | + process.executableURL = URL(fileURLWithPath: "/usr/bin/env") |
| 30 | + process.arguments = ["container", "version"] |
| 31 | + |
| 32 | + let pipe = Pipe() |
| 33 | + process.standardOutput = pipe |
| 34 | + process.standardError = FileHandle.nullDevice |
| 35 | + |
| 36 | + do { |
| 37 | + try process.run() |
| 38 | + process.waitUntilExit() |
| 39 | + |
| 40 | + guard process.terminationStatus == 0 else { return nil } |
| 41 | + |
| 42 | + let data = pipe.fileHandleForReading.readDataToEndOfFile() |
| 43 | + let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" |
| 44 | + |
| 45 | + // Parse "container version X.Y.Z (git: ...)" |
| 46 | + let versionPattern = #"container version (\d+\.\d+\.\d+)"# |
| 47 | + guard let range = output.range(of: versionPattern, options: .regularExpression) else { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + let versionString = String(output[range].dropFirst("container version ".count)) |
| 52 | + return versionString |
| 53 | + } catch { |
| 54 | + return nil |
| 55 | + } |
| 56 | + }() |
| 57 | + |
| 58 | + /// Get current runtime version |
| 59 | + public static var currentVersion: String { |
| 60 | + return cachedVersion ?? "0.10.0" // Default to 0.10.0 if detection fails |
| 61 | + } |
| 62 | + |
| 63 | + // MARK: - Feature Availability |
| 64 | + |
| 65 | + /// Check if a feature is available in current runtime |
| 66 | + public static func isAvailable(_ feature: Feature) -> Bool { |
| 67 | + return isVersionAtLeast(feature.minimumVersion) |
| 68 | + } |
| 69 | + |
| 70 | + /// Compare version strings (returns true if current >= required) |
| 71 | + public static func isVersionAtLeast(_ required: String) -> Bool { |
| 72 | + let current = currentVersion |
| 73 | + |
| 74 | + let currentParts = current.split(separator: ".").compactMap { Int($0) } |
| 75 | + let requiredParts = required.split(separator: ".").compactMap { Int($0) } |
| 76 | + |
| 77 | + guard !currentParts.isEmpty, !requiredParts.isEmpty else { |
| 78 | + return false |
| 79 | + } |
| 80 | + |
| 81 | + // Pad with zeros to equal length |
| 82 | + let maxLength = max(currentParts.count, requiredParts.count) |
| 83 | + let paddedCurrent = currentParts + Array(repeating: 0, count: maxLength - currentParts.count) |
| 84 | + let paddedRequired = requiredParts + Array(repeating: 0, count: maxLength - requiredParts.count) |
| 85 | + |
| 86 | + // Compare version parts |
| 87 | + for (current, required) in zip(paddedCurrent, paddedRequired) { |
| 88 | + if current > required { return true } |
| 89 | + if current < required { return false } |
| 90 | + } |
| 91 | + |
| 92 | + return true // Versions are equal |
| 93 | + } |
| 94 | + |
| 95 | + // MARK: - Conditional Execution |
| 96 | + |
| 97 | + /// Execute a block only if the feature is available |
| 98 | + public static func ifAvailable(_ feature: Feature, execute: () throws -> Void) rethrows { |
| 99 | + if isAvailable(feature) { |
| 100 | + try execute() |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// Execute a block with feature flag, providing fallback for older runtimes |
| 105 | + public static func withFeature<T>(_ feature: Feature, |
| 106 | + ifAvailable: () throws -> T, |
| 107 | + fallback: () throws -> T) rethrows -> T { |
| 108 | + if isAvailable(feature) { |
| 109 | + return try ifAvailable() |
| 110 | + } else { |
| 111 | + return try fallback() |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // MARK: - Environment Override |
| 116 | + |
| 117 | + /// Check if features are explicitly disabled via environment variable |
| 118 | + public static func isFeatureDisabled(_ feature: Feature) -> Bool { |
| 119 | + let envKey = "DISABLE_\(feature.rawValue.uppercased().replacingOccurrences(of: " ", with: "_"))" |
| 120 | + return ProcessInfo.processInfo.environment[envKey] == "1" |
| 121 | + } |
| 122 | + |
| 123 | + /// Force disable a feature (useful for testing) |
| 124 | + public static func disableFeature(_ feature: Feature) { |
| 125 | + let envKey = "DISABLE_\(feature.rawValue.uppercased().replacingOccurrences(of: " ", with: "_"))" |
| 126 | + setenv(envKey, "1", 1) |
| 127 | + } |
| 128 | +} |
0 commit comments