summaryrefslogtreecommitdiff
path: root/Sources/BubombCore/Executors/ShellCommandExecutor.swift
blob: 36c606b6ba0019f0a4064e2fa279d6247872bf20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import Foundation

/// Executes shell commands using an explicit shell or direct binary invocation.
public final class ShellCommandExecutor: @unchecked Sendable {
    private let runner = ProcessRunner()

    public init() {}

    public func execute(config: ShellConfig) async throws -> ExecutionResult {
        // Validate working directory if specified
        if let workDir = config.workingDirectory {
            _ = try PathValidator.validateWorkingDirectory(workDir).get()
        }

        let trimmedCommand = config.command.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !trimmedCommand.isEmpty else {
            throw ExecutionError.invalidConfiguration("Command cannot be empty.")
        }

        let executableURL: URL
        let arguments: [String]

        let shell = config.shellPath.trimmingCharacters(in: .whitespacesAndNewlines)
        if !shell.isEmpty {
            // Validate shell executable exists
            let validShell = try PathValidator.validateExecutable(at: shell).get()
            executableURL = URL(fileURLWithPath: validShell)

            var fullCommand = trimmedCommand
            if !config.arguments.isEmpty {
                let appended = config.arguments.map { arg -> String in
                    if arg.contains(" ") || arg.contains("\"") {
                        return "\"\(arg.replacingOccurrences(of: "\"", with: "\\\""))\""
                    }
                    return arg
                }.joined(separator: " ")
                fullCommand += " " + appended
            }
            arguments = ["-c", fullCommand]
        } else {
            // Direct binary invocation
            let components = trimmedCommand.components(separatedBy: .whitespaces).filter { !$0.isEmpty }
            guard let binaryName = components.first else {
                throw ExecutionError.invalidConfiguration("No binary specified.")
            }
            let validBinary = try PathValidator.validateExecutable(at: binaryName).get()
            executableURL = URL(fileURLWithPath: validBinary)
            arguments = Array(components.dropFirst()) + config.arguments
        }

        return try await runner.run(
            executableURL: executableURL,
            arguments: arguments,
            workingDirectory: config.workingDirectory,
            environment: config.environmentVariables,
            timeoutSeconds: config.timeoutSeconds
        )
    }

    public func cancel() {
        runner.cancel()
    }
}