feat: Adds output values to some of the commands to allow them to be piped into other commands

This commit is contained in:
2024-12-16 12:28:38 -05:00
parent 1302b15ee2
commit 1429c51821
11 changed files with 112 additions and 64 deletions

View File

@@ -15,7 +15,13 @@ public extension DependencyValues {
@DependencyClient
public struct VaultClient: Sendable {
public var run: @Sendable (RunOptions) async throws -> Void
public var run: Run
@DependencyClient
public struct Run: Sendable {
public var decrypt: @Sendable (RunOptions) async throws -> String
public var encrypt: @Sendable (RunOptions) async throws -> String
}
public struct RunOptions: Equatable, Sendable {
@@ -23,12 +29,10 @@ public struct VaultClient: Sendable {
public let loggingOptions: LoggingOptions
public let outputFilePath: String?
public let quiet: Bool
public let route: Route
public let shell: String?
public let vaultFilePath: String?
public init(
_ route: Route,
extraOptions: [String]? = nil,
loggingOptions: LoggingOptions,
outputFilePath: String? = nil,
@@ -40,28 +44,34 @@ public struct VaultClient: Sendable {
self.loggingOptions = loggingOptions
self.outputFilePath = outputFilePath
self.quiet = quiet
self.route = route
self.shell = shell
self.vaultFilePath = vaultFilePath
}
}
public enum Route: String, Equatable, Sendable {
case encrypt
case decrypt
@_spi(Internal)
public enum Route: String, Equatable, Sendable {
case encrypt
case decrypt
@_spi(Internal)
public var verb: String { rawValue }
}
public var verb: String { rawValue }
}
}
extension VaultClient: DependencyKey {
public static let testValue: VaultClient = Self()
public static let testValue: VaultClient = Self(run: Run())
public static var liveValue: VaultClient {
.init(
run: { try await $0.run() }
run: .init(
decrypt: {
try await $0.run(route: .decrypt)
},
encrypt: {
try await $0.run(route: .encrypt)
}
)
)
}
}
@@ -75,10 +85,18 @@ public extension VaultClient {
extension VaultClient.RunOptions {
func run() async throws {
// Sets up the default arguments and runs the `ansible-vault` command,
// returning the output file path, which is either supplied by the caller
// or found via the `fileClient.findVaultFileInCurrentDirectory`.
//
// This allows the output to be piped into other commands.
//
//
@discardableResult
func run(route: VaultClient.Route) async throws -> String {
@Dependency(\.commandClient) var commandClient
try await commandClient.run(
return try await commandClient.run(
logging: loggingOptions,
quiet: quiet,
shell: shell
@@ -87,6 +105,8 @@ extension VaultClient.RunOptions {
@Dependency(\.fileClient) var fileClient
@Dependency(\.logger) var logger
var output: String?
let configuration = try await configurationClient.findAndLoad()
logger.trace("Configuration: \(configuration)")
@@ -101,6 +121,7 @@ extension VaultClient.RunOptions {
guard let vaultFilePath else {
throw VaultClientError.vaultFileNotFound
}
output = vaultFilePath
logger.trace("Vault file: \(vaultFilePath)")
@@ -111,6 +132,7 @@ extension VaultClient.RunOptions {
if let outputFilePath {
arguments.append(contentsOf: ["--output", outputFilePath])
output = outputFilePath
}
if let extraOptions {
@@ -132,33 +154,11 @@ extension VaultClient.RunOptions {
logger.trace("Arguments: \(arguments)")
return arguments
return (arguments, output ?? "")
}
}
}
// extension VaultClient.RunOptions.Route {
//
// var arguments: [String] {
// let outputFile: String?
// var arguments: [String]
//
// switch self {
// case let .decrypt(outputFile: output):
// outputFile = output
// arguments = ["decrypt"]
// case let .encrypt(outputFile: output):
// outputFile = output
// arguments = ["encrypt"]
// }
//
// if let outputFile {
// arguments.append(contentsOf: ["--output", outputFile])
// }
// return arguments
// }
// }
enum VaultClientError: Error {
case vaultFileNotFound
}