This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from nodes-vapor/feature/more-env-features
Add more convenience for getting env variables
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
import Vapor | ||
|
||
public extension Environment { | ||
enum EnvironmentError: Error { | ||
case keyNotFound(key: String) | ||
} | ||
|
||
public static func get(_ key: String, _ fallback: String) -> String { | ||
return ProcessInfo.processInfo.environment[key] ?? fallback | ||
} | ||
|
||
public static func get(_ key: String) -> String? { | ||
return ProcessInfo.processInfo.environment[key] | ||
} | ||
|
||
public static func assertGet(_ key: String) throws -> String { | ||
guard let value = Environment.get(key) else { | ||
throw Environment.EnvironmentError.keyNotFound(key: key) | ||
} | ||
return value | ||
} | ||
} | ||
|
||
public func env(_ key: String, _ fallback: String) -> String { | ||
return Environment.get(key, fallback) | ||
} | ||
|
||
public func env(_ key: String) -> String? { | ||
return Environment.get(key) | ||
} | ||
|
||
public func assertEnv(_ key: String) throws -> String { | ||
return try Environment.assertGet(key) | ||
} |