28 lines
949 B
Swift
28 lines
949 B
Swift
import Foundation
|
|
|
|
public enum CrofSettingsReader {
|
|
public static let apiKeyEnvironmentKeys = ["CROF_API_KEY", "CROFAI_API_KEY"]
|
|
|
|
public static func apiKey(environment: [String: String] = ProcessInfo.processInfo.environment) -> String? {
|
|
for key in self.apiKeyEnvironmentKeys {
|
|
if let value = self.cleaned(environment[key]) {
|
|
return value
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
private 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
|
|
}
|
|
}
|