feat: Adds generate-config command.

This commit is contained in:
2024-11-29 20:32:32 -05:00
parent 84b002c997
commit 505f7d8013
8 changed files with 143 additions and 30 deletions

View File

@@ -16,6 +16,7 @@ public struct CliClient: Sendable {
public var encoder: @Sendable () -> JSONEncoder = { .init() }
public var loadConfiguration: @Sendable () throws -> Configuration
public var runCommand: @Sendable ([String], Bool, ShellCommand.Shell) async throws -> Void
public var createConfiguration: @Sendable (String) throws -> Void
public func runCommand(
quiet: Bool,
@@ -41,14 +42,14 @@ extension CliClient: DependencyKey {
encoder: JSONEncoder = .init(),
env: [String: String]
) -> Self {
.init {
@Dependency(\.fileClient) var fileClient
@Dependency(\.logger) var logger
return .init {
decoder
} encoder: {
encoder
} loadConfiguration: {
@Dependency(\.logger) var logger
@Dependency(\.fileClient) var fileClient
let urls = try findConfigurationFiles(env: env)
var env = env
@@ -75,6 +76,8 @@ extension CliClient: DependencyKey {
in: nil,
args
))
} createConfiguration: { path in
try fileClient.write(path, Data(Configuration.fileTemplate.utf8))
}
}

View File

@@ -40,4 +40,27 @@ public struct Configuration: Decodable {
let data = try encoder.encode(env)
return try decoder.decode(Configuration.self, from: data)
}
static var fileTemplate: String {
"""
# 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"
"""
}
}

View File

@@ -18,6 +18,7 @@ public struct FileClient: Sendable {
public var homeDir: @Sendable () -> URL = { URL(string: "~/")! }
public var isDirectory: @Sendable (URL) -> Bool = { _ in false }
public var isReadable: @Sendable (URL) -> Bool = { _ in false }
public var write: @Sendable (String, Data) throws -> Void
}
@_spi(Internal)
@@ -31,7 +32,10 @@ extension FileClient: DependencyKey {
loadFile: { try client.loadFile(at: $0, into: &$1, decoder: $2) },
homeDir: { client.homeDir },
isDirectory: { client.isDirectory(url: $0) },
isReadable: { client.isReadable(url: $0) }
isReadable: { client.isReadable(url: $0) },
write: { path, data in
try data.write(to: URL(filePath: path))
}
)
}