From e7f38330ace046dc8c956df629ed07fcda5fde21 Mon Sep 17 00:00:00 2001 From: jihoonahn Date: Sun, 9 Jun 2024 21:23:27 +0900 Subject: [PATCH] Update Task add log feature --- Sources/Command/Alias.swift | 11 ++++++----- Sources/Command/Task.swift | 34 ++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Sources/Command/Alias.swift b/Sources/Command/Alias.swift index 819c6bf5..56bda7f1 100644 --- a/Sources/Command/Alias.swift +++ b/Sources/Command/Alias.swift @@ -1,4 +1,4 @@ -/// An alias for a command that can be run with `PLCommand`. +/// An alias for a command that can be run with `Command`. public struct Alias { /// The URL of the executable to run. public let executableURL: String @@ -22,12 +22,12 @@ public struct Alias { public extension Alias { - /// Prepares a `PLCommand.Request` for the alias. + /// Prepares a `Command.Request` for the alias. /// /// - Parameters: /// - arguments: The arguments to pass to the command. /// - environment: The environment in which to run the command. - /// - Returns: A `PLCommand.Request` for the alias. + /// - Returns: A `Command.Request` for the alias. func prepare( _ arguments: Arguments? = nil, environment: Environment = .global @@ -49,9 +49,10 @@ public extension Alias { @discardableResult func run( _ arguments: Arguments? = nil, - environment: Environment = .global + environment: Environment = .global, + log: Bool = false ) -> Result { let req = prepare(arguments, environment: environment) - return Task.run(req) + return Task().run(req, log: log) } } diff --git a/Sources/Command/Task.swift b/Sources/Command/Task.swift index 58d554b9..dfcab6c7 100644 --- a/Sources/Command/Task.swift +++ b/Sources/Command/Task.swift @@ -1,10 +1,20 @@ import Foundation +import Logging + +/// The Task is Operation to execute Command. +public struct Task { + let logger: Logger? + + public init(logger: Logger? = nil) { + self.logger = logger + } -/// The Task is Operation to execute PLCommand. -public enum Task { /// running command. @discardableResult - static func run(_ request: Request) -> Result { + public func run( + _ request: Request, + log: Bool + ) -> Result { let process = prepare(request) let outputPipe = Pipe() @@ -21,9 +31,21 @@ public enum Task { if process.terminationStatus == EXIT_SUCCESS { let res = Response(statusCode: process.terminationStatus, output: outputActual, error: errorActual) + if let logger, log { + logger.debug( + "\(res.output)", + source: "command: \(request.arguments?.rawValue.joined(separator: " ") ?? "")" + ) + } return Result.success(request, res) } else { let res = Response(statusCode: process.terminationStatus, output: errorActual, error: errorActual) + if let logger, log { + logger.error( + "\(res.error)", + source: "command: \(request.arguments?.rawValue.joined(separator: " ") ?? "")" + ) + } return Result.failure(request, res) } } catch let error { @@ -35,7 +57,7 @@ public enum Task { public extension Task { /// Set the required parts before running the process - static func prepare(_ request: Request) -> Process { + func prepare(_ request: Request) -> Process { let process = Process() if #available(macOS 10.13, *) { process.executableURL = URL(path: request.executableURL) @@ -54,7 +76,7 @@ public extension Task { } /// FileHandle preprocessing - static func fileHandleData(fileHandle: FileHandle) throws -> String? { + func fileHandleData(fileHandle: FileHandle) throws -> String? { var data: Data? if #available(macOS 10.15.4, *) { data = try fileHandle.readToEnd() @@ -65,7 +87,7 @@ public extension Task { } /// Process preprocessing - static func run(process: Process) throws { + func run(process: Process) throws { if #available(macOS 10.13, *) { try process.run() } else {