mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
feat(config): add config center and runtime path resolution
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
package work.slhaf.partner.api.agent.runtime.config
|
||||||
|
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
object ConfigCenter {
|
||||||
|
|
||||||
|
val paths = resolvePaths()
|
||||||
|
private val registrations = mutableMapOf<Path, ConfigRegistration<out Config>>()
|
||||||
|
|
||||||
|
fun register(configurable: Configurable) {
|
||||||
|
|
||||||
|
fun normalizeRelativePath(path: Path): Path {
|
||||||
|
require(!path.isAbsolute) {
|
||||||
|
"Config path must be relative: $path"
|
||||||
|
}
|
||||||
|
return path.normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
val declared = configurable.declare()
|
||||||
|
val normalized = mutableMapOf<Path, ConfigRegistration<out Config>>()
|
||||||
|
|
||||||
|
declared.forEach { (path, registration) ->
|
||||||
|
val normalizedPath = normalizeRelativePath(path)
|
||||||
|
|
||||||
|
check(!normalized.containsKey(normalizedPath)) {
|
||||||
|
"Duplicated config path declared in the same configurable: $normalizedPath"
|
||||||
|
}
|
||||||
|
|
||||||
|
check(!registrations.containsKey(normalizedPath)) {
|
||||||
|
"Config path already registered: $normalizedPath"
|
||||||
|
}
|
||||||
|
|
||||||
|
normalized[normalizedPath] = registration
|
||||||
|
}
|
||||||
|
|
||||||
|
registrations.putAll(normalized)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class Config
|
||||||
|
|
||||||
|
interface Configurable {
|
||||||
|
fun declare(): Map<Path, ConfigRegistration<out Config>>
|
||||||
|
fun register() {
|
||||||
|
ConfigCenter.register(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ConfigRegistration<T : Config> {
|
||||||
|
fun type(): Class<T>
|
||||||
|
fun init(config: T)
|
||||||
|
fun onReload(config: T) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package work.slhaf.partner.api.agent.runtime.config
|
||||||
|
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
data class ConfigPaths(
|
||||||
|
val workDir: Path,
|
||||||
|
val configDir: Path,
|
||||||
|
val workspaceDir: Path,
|
||||||
|
val stateDir: Path,
|
||||||
|
val resourcesDir: Path
|
||||||
|
) {
|
||||||
|
fun ensureDirectories() {
|
||||||
|
Files.createDirectories(workDir)
|
||||||
|
Files.createDirectories(configDir)
|
||||||
|
Files.createDirectories(workspaceDir)
|
||||||
|
Files.createDirectories(stateDir)
|
||||||
|
Files.createDirectories(resourcesDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun resolvePaths(): ConfigPaths {
|
||||||
|
|
||||||
|
fun resolveWorkdir(): Path {
|
||||||
|
val envHome = System.getenv("PARTNER_HOME")?.trim()
|
||||||
|
if (!envHome.isNullOrEmpty()) {
|
||||||
|
return Paths.get(envHome).toAbsolutePath().normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
val userHome = System.getProperty("user.home")
|
||||||
|
?: throw IllegalStateException(" System property 'user.home' is missing.")
|
||||||
|
return Paths.get(userHome, ".partner").toAbsolutePath().normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
val workDir = resolveWorkdir()
|
||||||
|
return ConfigPaths(
|
||||||
|
workDir,
|
||||||
|
workDir.resolve("config"),
|
||||||
|
workDir.resolve("workspace"),
|
||||||
|
workDir.resolve("state"),
|
||||||
|
workDir.resolve("resources")
|
||||||
|
).apply { ensureDirectories() }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user