import Dependencies import Foundation import ShellClient /// Represents the configuration. public struct Configuration: Codable { public let playbookDir: String? public let inventoryPath: String? public let templateRepo: String? public let templateRepoVersion: String? public let templateDir: String? public let defaultPlaybookArgs: [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" } 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)") 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 { .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: ["--vault-id=myId@$SCRIPTS/vault-gopass-client"] ) } 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(key codingKey: Configuration.CodingKeys) -> String? { self[codingKey.rawValue] } }