Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #81 from nodes-vapor/feature/more-env-features
Browse files Browse the repository at this point in the history
Add more convenience for getting env variables
  • Loading branch information
steffendsommer authored Oct 24, 2018
2 parents 8c6bf10 + 815e3da commit af64a21
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Sources/Sugar/Helpers/Environment.swift
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)
}

0 comments on commit af64a21

Please sign in to comment.