Files
swift-hpa/Sources/ConfigurationClient/Configuration.swift

189 lines
5.6 KiB
Swift

import Foundation
/// Represents configurable settings for the command line tool.
public struct Configuration: Codable, Equatable, Sendable {
public let args: [String]?
public let useVaultArgs: Bool
public let playbook: Playbook?
public let template: Template
public let vault: Vault
public init(
args: [String]? = nil,
useVaultArgs: Bool = true,
playbook: Playbook? = nil,
template: Template = .init(),
vault: Vault = .init()
) {
self.args = args
self.useVaultArgs = useVaultArgs
self.playbook = playbook
self.template = template
self.vault = vault
}
public static var mock: Self {
.init(
args: [],
useVaultArgs: true,
playbook: nil,
template: .mock,
vault: .mock
)
}
public struct Playbook: Codable, Equatable, Sendable {
public let directory: String?
public let inventory: String?
public init(
directory: String? = nil,
inventory: String? = nil
) {
self.directory = directory
self.inventory = inventory
}
public static var mock: Self { .init() }
}
public struct Template: Codable, Equatable, Sendable {
public let url: String?
public let version: String?
public let directory: String?
public init(
url: String? = nil,
version: String? = nil,
directory: String? = nil
) {
self.url = url
self.version = version
self.directory = directory
}
public static var mock: Self {
.init(
url: "https://git.example.com/consult-template.git",
version: "1.0.0",
directory: "/path/to/local/template-directory"
)
}
}
public struct Vault: Codable, Equatable, Sendable {
public let args: [String]?
public let encryptId: String?
public init(
args: [String]? = nil,
encryptId: String? = nil
) {
self.args = args
self.encryptId = encryptId
}
public static var mock: Self {
.init(
args: [
"--vault-id=myId@$SCRIPTS/vault-gopass-client"
],
encryptId: "myId"
)
}
}
}
// public struct Configuration: Codable, Equatable, Sendable {
//
// public let playbookDir: String?
// public let inventoryPath: String?
// public let templateRepo: String?
// public let templateRepoVersion: String?
// public let templateDir: String?
// public let defaultPlaybookArgs: [String]?
// public let defaultVaultArgs: [String]?
// public let defaultVaultEncryptId: String?
//
// fileprivate 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"
// case defaultVaultArgs = "HPA_DEFAULT_VAULT_ARGS"
// case defaultVaultEncryptId = "HPA_DEFAULT_VAULT_ENCRYPT_ID"
// }
//
// public static func fromEnv(
// _ env: [String: String]
// ) throws -> Self {
// @Dependency(\.logger) var logger
//
// logger.trace("Creating configuration from env...")
//
// let hpaValues: [String: String] = env.filter { $0.key.contains("HPA") }
// logger.debug("HPA env vars: \(hpaValues)")
//
// return Configuration(
// playbookDir: hpaValues.value(for: .playbookDir),
// inventoryPath: hpaValues.value(for: .inventoryPath),
// templateRepo: hpaValues.value(for: .templateRepo),
// templateRepoVersion: hpaValues.value(for: .templateRepoVersion),
// templateDir: hpaValues.value(for: .templateDir),
// defaultPlaybookArgs: hpaValues.array(for: .defaultPlaybookArgs),
// defaultVaultArgs: hpaValues.array(for: .defaultVaultArgs),
// defaultVaultEncryptId: hpaValues.value(for: .defaultVaultEncryptId)
// )
// }
//
// static var mock: Self {
// .init(
// playbookDir: "/path/to/playbook",
// inventoryPath: "/path/to/inventory.ini",
// templateRepo: "https://git.example.com/consult-template.git",
// templateRepoVersion: "main",
// templateDir: "/path/to/local/template",
// defaultPlaybookArgs: ["--tags", "debug"],
// defaultVaultArgs: ["--vault-id=myId@$SCRIPTS/vault-gopass-client"],
// defaultVaultEncryptId: "myId"
// )
// }
//
// static var fileTemplate: String {
// """
// # vi: ft=sh
//
// # Example configuration, uncomment the lines and set the values appropriate for your
// # usage.
//
// # Set this to the location of the ansible-hpa-playbook on your local machine.
// #HPA_PLAYBOOK_DIR="/path/to/ansible-hpa-playbook"
//
// # Set this to the location of a template repository, which is used to create new assessment projects.
// #HPA_TEMPLATE_REPO="https://git.example.com/your/template.git"
//
// # Specify a branch, version, or sha of the template repository.
// #HPA_TEMPLATE_VERSION="main" # branch, version, or sha
//
// # Set this to a location of a template directory to use to create new projects.
// #HPA_TEMPLATE_DIR="/path/to/local/template"
//
// # Extra arguments that get passed directly to the ansible-playbook command.
// #HPA_DEFAULT_PLAYBOOK_ARGS="--vault-id=consults@$SCRIPTS/vault-gopass-client"
//
// """
// }
// }
//
// extension [String: String] {
// fileprivate func value(for codingKey: Configuration.CodingKeys) -> String? {
// self[codingKey.rawValue]
// }
//
// fileprivate func array(for codingKey: Configuration.CodingKeys) -> [String]? {
// value(for: codingKey).flatMap { $0.split(separator: ",").map(String.init) }
// }
// }