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

@@ -19,6 +19,30 @@ public struct CommandClient: Sendable {
/// Runs a shell command.
public var runCommand: @Sendable (RunCommandOptions) async throws -> Void
/// Runs a shell command, sets up logging, and returns an output value.
///
/// This is useful when you need to give some output value to the caller,
/// generally for the ability of that output to be piped into other commands.
///
@discardableResult
public func run<T>(
logging logginOptions: LoggingOptions,
quiet: Bool = false,
shell: String? = nil,
in workingDirectory: String? = nil,
makeArguments: @Sendable @escaping () async throws -> ([String], T)
) async throws -> T {
try await logginOptions.withLogger {
let (arguments, returnValue) = try await makeArguments()
try await self.run(
quiet: quiet,
shell: shell,
arguments
)
return returnValue
}
}
/// Runs a shell command and sets up logging.
public func run(
logging logginOptions: LoggingOptions,
@@ -27,14 +51,13 @@ public struct CommandClient: Sendable {
in workingDirectory: String? = nil,
makeArguments: @Sendable @escaping () async throws -> [String]
) async throws {
try await logginOptions.withLogger {
let arguments = try await makeArguments()
try await self.run(
quiet: quiet,
shell: shell,
arguments
)
}
try await run(
logging: logginOptions,
quiet: quiet,
shell: shell,
in: workingDirectory,
makeArguments: { try await (makeArguments(), ()) }
)
}
/// Runs a shell command.