Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Sources/NestKit/Utils/ProcessExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,42 @@ import os

public protocol ProcessExecutor: Sendable {
func execute(command: String, _ arguments: [String]) async throws -> String

/// Executes the given command with the given arguments.
/// All inputs, outputs and errors are exposed to users unlike ``execute(command:_:)``.
/// So user can input texts if the command requires.
/// The returned value indicates the status of the results of the command.
func executeInteractively(command: String, _ arguments: [String]) async throws -> Int32
}

extension ProcessExecutor {
public func execute(command: String, _ arguments: String...) async throws -> String {
try await execute(command: command, arguments)
}

public func executeInteractively(command: String, _ arguments: String...) async throws -> Int32 {
try await executeInteractively(command: command, arguments)
}

public func which(_ command: String) async throws -> String {
try await execute(command: "/usr/bin/which", command)
}
}

public struct NestProcessExecutor: ProcessExecutor {
let currentDirectoryURL: URL?
let environment: [String: String]
let logger: Logging.Logger
let logLevel: Logging.Logger.Level

public init(currentDirectory: URL? = nil, logger: Logging.Logger, logLevel: Logging.Logger.Level = .debug) {
public init(
currentDirectory: URL? = nil,
environment: [String: String] = ProcessInfo.processInfo.environment,
logger: Logging.Logger,
logLevel: Logging.Logger.Level = .debug
) {
self.currentDirectoryURL = currentDirectory
self.environment = environment
self.logger = logger
self.logLevel = logLevel
}
Expand All @@ -48,6 +65,7 @@ public struct NestProcessExecutor: ProcessExecutor {
process.currentDirectoryURL = currentDirectoryURL
process.executableURL = executableURL
process.arguments = arguments
process.environment = environment

let outputPipe = Pipe()
process.standardOutput = outputPipe
Expand Down Expand Up @@ -96,6 +114,28 @@ public struct NestProcessExecutor: ProcessExecutor {
}
}
}

public func executeInteractively(command: String, _ arguments: [String]) async throws -> Int32 {
logger.debug("$ \(command) \(arguments.joined(separator: " "))")

let process = Process()
process.executableURL = URL(fileURLWithPath: command)
process.arguments = arguments
process.environment = environment

if let currentDirectoryURL {
process.currentDirectoryURL = currentDirectoryURL
}

try process.run()

// Need to support standard input
// https://forums.swift.org/t/how-to-allow-process-to-receive-user-input-when-run-as-part-of-an-executable-e-g-to-enabled-sudo-commands/34357/7
tcsetpgrp(STDIN_FILENO, process.processIdentifier)

process.waitUntilExit()
return process.terminationStatus
}
}

enum StreamElement {
Expand Down
6 changes: 6 additions & 0 deletions Sources/NestTestHelpers/MockExecutorBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public struct MockProcessExecutor: ProcessExecutor {
public func execute(command: String, _ arguments: [String]) async throws -> String {
try await executorClosure(command, arguments)
}

public func executeInteractively(command: String, _ arguments: [String]) async throws -> Int32 {
// For testing, just call execute and exit
_ = try await executorClosure(command, arguments)
return 0
}
}
16 changes: 10 additions & 6 deletions Sources/nest/Commands/RunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ struct RunCommand: AsyncParsableCommand {
return
}

let binaryRelativePath = executables[0].binaryPath // FIXME: Needs to address multiple commands in the same artifact bundle.
_ = try? await NestProcessExecutor(logger: logger, logLevel: .info)
.execute(
command: nestDirectory.rootDirectory.appending(path: binaryRelativePath.path(percentEncoded: false)).path(percentEncoded: false),
subcommand.arguments
)
// FIXME: Needs to address multiple commands in the same artifact bundle.
let binaryRelativePath = executables[0].binaryPath.path(percentEncoded: false)
let command = nestDirectory.rootDirectory.appending(path: binaryRelativePath).path(percentEncoded: false)
var environment = ProcessInfo.processInfo.environment
environment["RESOURCE_PATH"] = ""
let result = try await NestProcessExecutor(environment: environment, logger: logger, logLevel: .info)
.executeInteractively(command: command, subcommand.arguments)
if result != 0 {
Foundation.exit(result)
}
}
}

Expand Down