-
Notifications
You must be signed in to change notification settings - Fork 3
/
LocalProperties.kt
42 lines (37 loc) · 1.16 KB
/
LocalProperties.kt
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
package com.beust.koolaid
import com.google.inject.Singleton
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
/**
* Encapsulate read access to local.properties.
*/
@Singleton
class LocalProperties {
private val localProperties: Properties by lazy {
Properties().apply {
Paths.get("local.properties").let { path ->
if (path.toFile().exists()) {
Files.newInputStream(path).use {
load(it)
}
}
}
}
}
fun get(p: LocalProperty) : String? {
val result = localProperties.getProperty(p.name)
if (result != null && p.allowed.any() && ! p.allowed.contains(result)) {
throw IllegalArgumentException("Illegal value for \"$p\": \"$result\", allowed values: ${p.allowed}")
}
return result
}
}
enum class Database(val value: String) {
POSTGRESQL("postgresql"), IN_MEMORY("inMemory")
}
enum class LocalProperty(val allowed: Set<String> = hashSetOf()) {
DATABASE(setOf(Database.POSTGRESQL.value, Database.IN_MEMORY.value)),
DATABASE_USER,
DATABASE_PASSWORD;
}