You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.1 KiB
Kotlin

package me.eater.hefbrug.logging.message
import me.eater.hefbrug.access.ExecutionOutput
import me.eater.hefbrug.definition.DefinitionKey
import me.eater.hefbrug.logging.LogFormat
import me.eater.hefbrug.node.Node
import me.eater.hefbrug.utils.escape
import me.eater.hefbrug.utils.handlebars
object Messages {
fun collectingCurrentState(key: DefinitionKey, node: Node) =
HefbrugMessage("Collecting current state for {{key}} for {{node}}", "key" to key, "node" to node)
fun applying(key: DefinitionKey, node: Node) =
HefbrugMessage("Applying {{key}} for {{node}}", "key" to key, "node" to node)
fun startEnforcing(key: DefinitionKey, node: Node) =
HefbrugMessage("Enforcing desired state for {{key}} for {{node}}", "key" to key, "node" to node)
fun foundDifference(key: DefinitionKey, node: Node, differences: Map<String, Pair<Any?, Any?>>) =
HefbrugMessage(
"enforcing {{key}} on {{node}} to desired state {{diff}}",
"key" to key,
"node" to node,
"diff" to Diff(differences)
)
fun noDifferences(key: DefinitionKey, node: Node) =
HefbrugMessage(
"{{key}} on {{node}} is in desired state",
"key" to key,
"node" to node
)
fun failedDefinition(
key: DefinitionKey,
node: Node,
executionOutput: ExecutionOutput
) =
HefbrugMessage(
"Failed enforcing state {{key}} for {{node}} with command [@|yellow {{cmd}}|@] with exit code @|red {{exitcode}}|@",
"key" to key,
"node" to node,
"cmd" to executionOutput.command.command.joinToString(" ") { it.escape() },
"exitcode" to executionOutput.exitCode,
"execution" to executionOutput
)
class Diff(val body: Map<String, Pair<Any?, Any?>>) : LogFormat {
override fun logFormat(): String {
return "[${body.map { (k, v) ->
val (old, new) = v
"@|yellow $k|@: @|red {{old}}|@ => @|green {{new}}|@".handlebars("old" to old, "new" to new)
}.joinToString(", ")}]"
}
}
}