Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions Sources/CLI/main/AppleDocs.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import ArgumentParser
import Logging

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#endif

@main
enum AppleDocs {
private static let logger = Logger(label: "com.techprimate.apple-docs")
Expand All @@ -17,11 +9,8 @@ enum AppleDocs {
static func main() async {
let telemetry = Dependencies.shared.telemetry
telemetry.start()
// Ignore SIGPIPE so writing to a closed pipe (e.g. `apple-docs ... | head`)
// fails with EPIPE instead of terminating the process.
// Must stay after `telemetry.start()`: the crash handler installs its own
// SIGPIPE handler during start, which would otherwise replace SIG_IGN.
signal(SIGPIPE, SIG_IGN)
// Telemetry installs a SIGPIPE handler during start, so configure the terminal afterward.
Dependencies.shared.terminalSetup.configure()
var loggingConfigured = false

do {
Expand Down Expand Up @@ -49,6 +38,9 @@ enum AppleDocs {
}

private static func verboseLoggingEnabled(for command: any ParsableCommand) -> Bool {
(command as? any GlobalOptionsProviding)?.global.verbose ?? false
guard let optionsProvider = command as? any GlobalOptionsProviding else {
return false
}
return optionsProvider.global.verbose
}
}
1 change: 1 addition & 0 deletions Sources/CLI/main/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct Dependencies: Sendable {
static let shared = Dependencies()

let terminalCapabilities = DefaultTerminalCapabilities()
let terminalSetup = TerminalSetup()
let telemetry = DefaultTelemetry(
// Telemetry starts before SwiftLog is bootstrapped. Resolve its logger only when logging an event.
logger: { Logger(label: "com.techprimate.apple-docs.telemetry") },
Expand Down
14 changes: 14 additions & 0 deletions Sources/CLI/main/TerminalSetup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#endif

struct TerminalSetup {
func configure() {
// Ignore SIGPIPE so writing to a closed pipe fails with EPIPE instead of terminating.
signal(SIGPIPE, SIG_IGN)
}
}
Loading