added Properties to created Tasks
This commit is contained in:
1 parent
99a494608f
commit
8078e34a2c
10 files changed
+792
-51
No files matched your search
@@ -1,22 +1,43 @@
|
||||
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(),
|
||||
)
|
||||
|
||||
fun loadConfig(): Configuration {
|
||||
data class ConfigResult(
|
||||
val config: Configuration,
|
||||
val sourceFile: File?,
|
||||
)
|
||||
|
||||
fun loadConfig(): ConfigResult {
|
||||
val yaml = Yaml.default
|
||||
val serializer = Configuration.serializer()
|
||||
|
||||
@@ -25,14 +46,27 @@ fun loadConfig(): Configuration {
|
||||
File(System.getProperty("user.home"), ".anytask/anytask.yaml"),
|
||||
File("anytask.yaml"),
|
||||
)
|
||||
externalLocations.firstOrNull { it.exists() }?.let {
|
||||
return yaml.decodeFromString(serializer, it.readText())
|
||||
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("/anytask.yaml")?.let {
|
||||
return yaml.decodeFromString(serializer, it.bufferedReader().readText())
|
||||
Configuration::class.java.getResourceAsStream("/anytask.yaml")?.let { stream ->
|
||||
val config = yaml.decodeFromString(serializer, stream.bufferedReader().readText())
|
||||
logger.info("Loaded config from classpath:/anytask.yaml")
|
||||
return ConfigResult(config, null)
|
||||
}
|
||||
|
||||
return Configuration()
|
||||
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/anytask.yaml")
|
||||
file.parentFile?.mkdirs()
|
||||
val yaml = Yaml.default
|
||||
file.writeText(yaml.encodeToString(Configuration.serializer(), config))
|
||||
logger.info("Saved config to ${file.absolutePath}")
|
||||
}
|
||||