39 lines
1.1 KiB
Swift
39 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
public struct SyntheticSettingsReader: Sendable {
|
|
public static let apiKeyKey = "SYNTHETIC_API_KEY"
|
|
|
|
public static func apiKey(
|
|
environment: [String: String] = ProcessInfo.processInfo.environment) -> String?
|
|
{
|
|
if let token = self.cleaned(environment[apiKeyKey]) { return token }
|
|
return nil
|
|
}
|
|
|
|
static func cleaned(_ raw: String?) -> String? {
|
|
guard var value = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else {
|
|
return nil
|
|
}
|
|
|
|
if (value.hasPrefix("\"") && value.hasSuffix("\"")) ||
|
|
(value.hasPrefix("'") && value.hasSuffix("'"))
|
|
{
|
|
value = String(value.dropFirst().dropLast())
|
|
}
|
|
|
|
value = value.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
return value.isEmpty ? nil : value
|
|
}
|
|
}
|
|
|
|
public enum SyntheticSettingsError: LocalizedError, Sendable {
|
|
case missingToken
|
|
|
|
public var errorDescription: String? {
|
|
switch self {
|
|
case .missingToken:
|
|
"Synthetic API key not found. Set apiKey in ~/.codexbar/config.json or SYNTHETIC_API_KEY."
|
|
}
|
|
}
|
|
}
|