dev.forkhandles.bunting.Config.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bunting4k Show documentation
Show all versions of bunting4k Show documentation
ForkHandles CLI Flag library
package dev.forkhandles.bunting
import java.io.File
import java.util.Properties
interface Config {
operator fun get(key: String): String?
operator fun set(key: String, value: String)
}
class InMemoryConfig : Config {
private val map = mutableMapOf()
override fun get(key: String) = map[key]
override fun set(key: String, value: String) {
map[key] = value
}
}
class PropertiesFileConfig(private val location: File) : Config {
private val properties: Properties by lazy { Properties().apply { if (location.exists()) location.reader().use(this::load) } }
override fun get(key: String): String? = properties.getProperty(key)
override fun set(key: String, value: String) {
properties.setProperty(key, value).also {
location.parentFile.mkdirs()
location.writer().use { properties.store(it, "") }
}
}
}
fun configFile(name: String) = File("${System.getProperty("user.home")}/.$name/config")
© 2015 - 2025 Weber Informatics LLC | Privacy Policy