diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/Container-Compose.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/Container-Compose.xcscheme index c9d1c50..1af42bf 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/Container-Compose.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/Container-Compose.xcscheme @@ -106,7 +106,7 @@ isEnabled = "YES"> diff --git a/Sample Compose Files/Healthchecked Redis/docker-compose.yaml b/Sample Compose Files/Healthchecked Redis/docker-compose.yaml new file mode 100644 index 0000000..d8c5ce6 --- /dev/null +++ b/Sample Compose Files/Healthchecked Redis/docker-compose.yaml @@ -0,0 +1,12 @@ +services: + redis: + restart: always + image: redis:7-alpine + volumes: + - redis-data:/data + healthcheck: + test: "redis-cli ping" + interval: 5s + retries: 20 + ports: + - "6379:6379" diff --git a/Sources/Container-Compose/Codable Structs/Healthcheck.swift b/Sources/Container-Compose/Codable Structs/Healthcheck.swift index c26e23a..b7e8fc5 100644 --- a/Sources/Container-Compose/Codable Structs/Healthcheck.swift +++ b/Sources/Container-Compose/Codable Structs/Healthcheck.swift @@ -48,4 +48,22 @@ public struct Healthcheck: Codable, Hashable { self.retries = retries self.timeout = timeout } + + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + // Handle if `test` is a single string instead of an array + if let test = try? container.decodeIfPresent([String].self, forKey: .test) { + self.test = test + } else if let testString = try? container.decodeIfPresent(String.self, forKey: .test) { + self.test = ["CMD-SHELL", testString] + } else { + self.test = nil + } + + self.start_period = try container.decodeIfPresent(String.self, forKey: .start_period) + self.interval = try container.decodeIfPresent(String.self, forKey: .interval) + self.retries = try container.decodeIfPresent(Int.self, forKey: .retries) + self.timeout = try container.decodeIfPresent(String.self, forKey: .timeout) + } } diff --git a/Tests/Container-Compose-StaticTests/HealthcheckConfigurationTests.swift b/Tests/Container-Compose-StaticTests/HealthcheckConfigurationTests.swift index 72ecf8b..eb2c6c9 100644 --- a/Tests/Container-Compose-StaticTests/HealthcheckConfigurationTests.swift +++ b/Tests/Container-Compose-StaticTests/HealthcheckConfigurationTests.swift @@ -35,6 +35,20 @@ struct HealthcheckConfigurationTests { #expect(healthcheck.test?.first == "CMD") } + @Test("Parse healthcheck with test command (string)") + func parseHealthcheckWithTestAsSingleString() throws { + let yaml = """ + test: "redis-cli ping" + """ + + let decoder = YAMLDecoder() + let healthcheck = try decoder.decode(Healthcheck.self, from: yaml) + + #expect(healthcheck.test?.count == 2) + #expect(healthcheck.test?.first == "CMD-SHELL") + #expect(healthcheck.test?[1] == "redis-cli ping") + } + @Test("Parse healthcheck with interval") func parseHealthcheckWithInterval() throws { let yaml = """