Skip to content

Commit

Permalink
fix: use Foundation.ProcessInfo for environment parsing
Browse files Browse the repository at this point in the history
- replaces use of `environ` with Foundation
  • Loading branch information
GNMoseke committed Sep 30, 2024
1 parent 56336d7 commit 564f624
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions Sources/Hummingbird/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Darwin.C
#endif
import HummingbirdCore
import NIOCore
import Foundation

/// Access environment variables
public struct Environment: Sendable, Decodable, ExpressibleByDictionaryLiteral {
Expand Down Expand Up @@ -111,22 +112,8 @@ public struct Environment: Sendable, Decodable, ExpressibleByDictionaryLiteral {
/// Construct environment variable map
static func getEnvironment() -> [String: String] {
var values: [String: String] = [:]
let equalSign = Character("=")
#if canImport(Musl)
guard let envp = environ else { return [:] }
#else
let envp = environ
#endif
var idx = 0

while let entry = envp.advanced(by: idx).pointee {
let entry = String(cString: entry)
if let i = entry.firstIndex(of: equalSign) {
let key = String(entry.prefix(upTo: i))
let value = String(entry.suffix(from: i).dropFirst())
values[key.lowercased()] = value
}
idx += 1
ProcessInfo.processInfo.environment.keys.forEach { key in
values[key.lowercased()] = ProcessInfo.processInfo.environment[key]
}
return values
}
Expand Down

0 comments on commit 564f624

Please sign in to comment.