|
| 1 | +import ArgumentParser |
| 2 | +import FileSystem |
| 3 | +import Foundation |
| 4 | +import Logging |
| 5 | +import Path |
| 6 | +import ServiceContextModule |
| 7 | +import VirtualOSEnvironment |
| 8 | +import VirtualOSEnvironmentInterface |
| 9 | +import VirtualOSLogging |
| 10 | + |
| 11 | +@main |
| 12 | +private enum VirtualOS { |
| 13 | + static func main() async throws { |
| 14 | + let fileSystem = FileSystem() |
| 15 | + let environment = try await Environment.current() |
| 16 | + let logFilePath = environment.cacheDirectory.appending(components: ["logs", "\(UUID().uuidString).log"]) |
| 17 | + if try await !fileSystem.exists(logFilePath.parentDirectory) { |
| 18 | + try await fileSystem.makeDirectory(at: logFilePath.parentDirectory) |
| 19 | + } |
| 20 | + let logger = Logger.makeDefaultLogger(logFilePath: logFilePath) |
| 21 | + var serviceContext = ServiceContext.topLevel |
| 22 | + serviceContext.logger = logger |
| 23 | + serviceContext.environment = environment |
| 24 | + |
| 25 | + await ServiceContext.$current.withValue(serviceContext) { |
| 26 | + defer { |
| 27 | + showCompletion(logFilePath: logFilePath) |
| 28 | + } |
| 29 | + |
| 30 | + do { |
| 31 | + var command = try VirtualOSCommand.parseAsRoot() |
| 32 | + if var asyncCommand = command as? AsyncParsableCommand { |
| 33 | + try await asyncCommand.run() |
| 34 | + } else { |
| 35 | + try command.run() |
| 36 | + } |
| 37 | + } catch { |
| 38 | + showCompletion(logFilePath: logFilePath) |
| 39 | + VirtualOSCommand.exit(withError: error) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static func showCompletion(logFilePath: AbsolutePath) { |
| 45 | + if !CommandLine.arguments.contains("--help"), !CommandLine.arguments.contains("-h") { |
| 46 | + print("Logs available at: \(logFilePath.pathString)") |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments