Skip to content

Commit

Permalink
Update Task add log feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoonahn committed Jun 9, 2024
1 parent 2bf0242 commit e7f3833
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
11 changes: 6 additions & 5 deletions Sources/Command/Alias.swift
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
}
}
34 changes: 28 additions & 6 deletions Sources/Command/Task.swift
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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 {
Expand Down

0 comments on commit e7f3833

Please sign in to comment.