-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Environment+Process.swift
53 lines (45 loc) 路 1.8 KB
/
Environment+Process.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
extension Environment {
/// The process information of an environment. Wraps `ProcessInto.processInfo`.
@dynamicMemberLookup public struct Process {
/// The process information of the environment.
private let _info: ProcessInfo
/// Creates a new `Process` wrapper for process information.
///
/// - parameter info: The process info that the wrapper accesses. Defaults to `ProcessInto.processInfo`.
internal init(info: ProcessInfo = .processInfo) {
self._info = info
}
/// Gets a variable's value from the process' environment, and converts it to generic type `T`.
///
/// Environment.process.DATABASE_PORT = 3306
/// Environment.process.DATABASE_PORT // 3306
public subscript<T>(dynamicMember member: String) -> T? where T: LosslessStringConvertible {
get {
return self._info.environment[member].flatMap { T($0) }
}
nonmutating set (value) {
if let raw = value?.description {
setenv(member, raw, 1)
} else {
unsetenv(member)
}
}
}
/// Gets a variable's value from the process' environment as a `String`.
///
/// Environment.process.DATABASE_USER = "root"
/// Environment.process.DATABASE_USER // "root"
public subscript(dynamicMember member: String) -> String? {
get {
return self._info.environment[member]
}
nonmutating set (value) {
if let raw = value {
setenv(member, raw, 1)
} else {
unsetenv(member)
}
}
}
}
}