mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 13:31:32 +00:00
Implement common configuration loading mechanism.
This commit is contained in:
@ -1,7 +1,10 @@
|
||||
package gay.pizza.foundation.shared
|
||||
|
||||
import com.charleskorn.kaml.Yaml
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import org.slf4j.Logger
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.inputStream
|
||||
|
||||
/**
|
||||
* Copy the default configuration from the resource [resourceName] into the directory [targetPath].
|
||||
@ -20,7 +23,6 @@ inline fun <reified T> copyDefaultConfig(log: Logger, targetPath: Path, resource
|
||||
val outPath = targetPath.resolve(resourceName)
|
||||
val outFile = outPath.toFile()
|
||||
if (outFile.exists()) {
|
||||
log.debug("Configuration file already exists.")
|
||||
return outPath
|
||||
}
|
||||
|
||||
@ -37,3 +39,13 @@ inline fun <reified T> copyDefaultConfig(log: Logger, targetPath: Path, resource
|
||||
|
||||
return outPath
|
||||
}
|
||||
|
||||
inline fun <reified T> loadConfigurationWithDefault(
|
||||
log: Logger,
|
||||
deserializer: DeserializationStrategy<T>,
|
||||
pluginConfigDirectoryPath: Path,
|
||||
name: String
|
||||
): T {
|
||||
val path = copyDefaultConfig<T>(log, pluginConfigDirectoryPath, name)
|
||||
return Yaml.default.decodeFromStream(deserializer, path.inputStream())
|
||||
}
|
Reference in New Issue
Block a user