tart/Sources/tart/Root.swift

49 lines
1.1 KiB
Swift

import ArgumentParser
import Foundation
@main
struct Root: AsyncParsableCommand {
static var configuration = CommandConfiguration(
commandName: "tart",
version: CI.version,
subcommands: [
Create.self,
Clone.self,
Run.self,
Set.self,
List.self,
Login.self,
IP.self,
Pull.self,
Push.self,
Prune.self,
Delete.self,
])
public static func main() async throws {
// Ensure the default SIGINT handled is disabled,
// otherwise there's a race between two handlers
signal(SIGINT, SIG_IGN);
// Handle cancellation by Ctrl+C ourselves
let task = withUnsafeCurrentTask { $0 }!
let sigintSrc = DispatchSource.makeSignalSource(signal: SIGINT)
sigintSrc.setEventHandler {
task.cancel()
}
sigintSrc.activate()
// Parse and run command
do {
var command = try parseAsRoot()
if var asyncCommand = command as? AsyncParsableCommand {
try await asyncCommand.run()
} else {
try command.run()
}
} catch {
exit(withError: error)
}
}
}