feat: Reorganizes files
This commit is contained in:
@@ -10,9 +10,9 @@ public struct Configuration: Codable {
|
||||
public let templateRepo: String?
|
||||
public let templateRepoVersion: String?
|
||||
public let templateDir: String?
|
||||
public let defaultPlaybookArgs: String?
|
||||
public let defaultPlaybookArgs: [String]?
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
fileprivate enum CodingKeys: String, CodingKey {
|
||||
case playbookDir = "HPA_PLAYBOOK_DIR"
|
||||
case inventoryPath = "HPA_DEFAULT_INVENTORY"
|
||||
case templateRepo = "HPA_TEMPLATE_REPO"
|
||||
@@ -22,23 +22,26 @@ public struct Configuration: Codable {
|
||||
}
|
||||
|
||||
public static func fromEnv(
|
||||
_ env: [String: String],
|
||||
encoder: JSONEncoder = .init(),
|
||||
decoder: JSONDecoder = .init()
|
||||
_ env: [String: String]
|
||||
) 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
|
||||
}
|
||||
}
|
||||
let hpaValues: [String: String] = env.filter { $0.key.contains("HPA") }
|
||||
logger.debug("HPA env vars: \(hpaValues)")
|
||||
let data = try encoder.encode(env)
|
||||
return try decoder.decode(Configuration.self, from: data)
|
||||
|
||||
let defaultArgs: [String]? = hpaValues.value(key: .defaultPlaybookArgs)
|
||||
.flatMap { $0.split(separator: ",").map(String.init) }
|
||||
|
||||
return Configuration(
|
||||
playbookDir: hpaValues.value(key: .playbookDir),
|
||||
inventoryPath: hpaValues.value(key: .inventoryPath),
|
||||
templateRepo: hpaValues.value(key: .templateRepo),
|
||||
templateRepoVersion: hpaValues.value(key: .templateRepoVersion),
|
||||
templateDir: hpaValues.value(key: .templateDir),
|
||||
defaultPlaybookArgs: defaultArgs
|
||||
)
|
||||
}
|
||||
|
||||
static var mock: Self {
|
||||
@@ -48,7 +51,7 @@ public struct Configuration: Codable {
|
||||
templateRepo: "https://git.example.com/consult-template.git",
|
||||
templateRepoVersion: "main",
|
||||
templateDir: "/path/to/local/template",
|
||||
defaultPlaybookArgs: "--vault-id=myId@$SCRIPTS/vault-gopass-client"
|
||||
defaultPlaybookArgs: ["--vault-id=myId@$SCRIPTS/vault-gopass-client"]
|
||||
)
|
||||
}
|
||||
|
||||
@@ -77,3 +80,9 @@ public struct Configuration: Codable {
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
extension [String: String] {
|
||||
fileprivate func value(key codingKey: Configuration.CodingKeys) -> String? {
|
||||
self[codingKey.rawValue]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user