Files
swift-hpa/Sources/CliClient/Configuration.swift
2024-11-29 14:30:52 -05:00

44 lines
1.3 KiB
Swift

import Dependencies
import Foundation
import ShellClient
/// Represents the configuration.
public struct Configuration: Decodable {
public let playbookDir: String?
public let inventoryPath: String?
public let templateRepo: String?
public let templateRepoVersion: String?
public let templateDir: String?
public let defaultPlaybookArgs: String?
private enum CodingKeys: String, CodingKey {
case playbookDir = "HPA_PLAYBOOK_DIR"
case inventoryPath = "HPA_DEFAULT_INVENTORY"
case templateRepo = "HPA_TEMPLATE_REPO"
case templateRepoVersion = "HPA_TEMPLATE_VERSION"
case templateDir = "HPA_TEMPLATE_DIR"
case defaultPlaybookArgs = "HPA_DEFAULT_PLAYBOOK_ARGS"
}
public static func fromEnv(
_ env: [String: String],
encoder: JSONEncoder = .init(),
decoder: JSONDecoder = .init()
) throws -> Self {
@Dependency(\.logger) var logger
logger.trace("Creating configuration from env...")
// logger.debug("\(env)")
let hpaValues = env.reduce(into: [String: String]()) { partial, next in
if next.key.contains("HPA") {
partial[next.key] = next.value
}
}
logger.debug("HPA env vars: \(hpaValues)")
let data = try encoder.encode(env)
return try decoder.decode(Configuration.self, from: data)
}
}