73 lines
2.3 KiB
Kotlin
73 lines
2.3 KiB
Kotlin
package ch.ergon.anytask.config
|
|
|
|
import com.charleskorn.kaml.Yaml
|
|
import kotlinx.serialization.SerialName
|
|
import kotlinx.serialization.Serializable
|
|
import org.apache.logging.log4j.LogManager
|
|
import java.io.File
|
|
|
|
private val logger = LogManager.getLogger("Configuration")
|
|
|
|
@Serializable
|
|
data class Connection(
|
|
val apiKey: String = "",
|
|
val baseUrl: String = "http://localhost:31009",
|
|
)
|
|
|
|
@Serializable
|
|
data class PropertyConfig(
|
|
val key: String,
|
|
val name: String = "",
|
|
val format: String = "",
|
|
val indicator: String = "",
|
|
@SerialName("possible_values") val possibleValues: List<String> = emptyList(),
|
|
@SerialName("object_type_key") val objectTypeKey: String = "",
|
|
)
|
|
|
|
@Serializable
|
|
data class Configuration(
|
|
val connection: Connection = Connection(),
|
|
val spaceId: String = "",
|
|
@SerialName("task_type_key") val taskTypeKey: String = "",
|
|
val properties: List<PropertyConfig> = emptyList(),
|
|
)
|
|
|
|
data class ConfigResult(
|
|
val config: Configuration,
|
|
val sourceFile: File?,
|
|
)
|
|
|
|
fun loadConfig(): ConfigResult {
|
|
val yaml = Yaml.default
|
|
val serializer = Configuration.serializer()
|
|
|
|
// External files take precedence (production use)
|
|
val externalLocations = listOf(
|
|
File(System.getProperty("user.home"), ".anytask/application.yaml"),
|
|
File("application.yaml"),
|
|
)
|
|
externalLocations.firstOrNull { it.exists() }?.let { file ->
|
|
val config = yaml.decodeFromString(serializer, file.readText())
|
|
logger.info("Loaded config from ${file.absolutePath}")
|
|
return ConfigResult(config, file)
|
|
}
|
|
|
|
// Fallback: classpath resource (development)
|
|
Configuration::class.java.getResourceAsStream("/application.yaml")?.let { stream ->
|
|
val config = yaml.decodeFromString(serializer, stream.bufferedReader().readText())
|
|
logger.info("Loaded config from classpath:/anytask.yaml")
|
|
return ConfigResult(config, null)
|
|
}
|
|
|
|
logger.warn("No config found, using defaults")
|
|
return ConfigResult(Configuration(), null)
|
|
}
|
|
|
|
fun saveConfig(config: Configuration, sourceFile: File?) {
|
|
val file = sourceFile ?: File(System.getProperty("user.home"), ".anytask/application.yaml")
|
|
file.parentFile?.mkdirs()
|
|
val yaml = Yaml.default
|
|
file.writeText(yaml.encodeToString(Configuration.serializer(), config))
|
|
logger.info("Saved config to ${file.absolutePath}")
|
|
}
|