|
#!/usr/bin/env -S kotlinc -jvm-target 1.8 -script |
|
|
|
@file:DependsOn("com.github.ajalt.clikt:clikt-jvm:3.1.0") |
|
@file:DependsOn("org.jooq:joox-java-6:1.6.2") |
|
|
|
import Modifier_main.ArgType.* |
|
import com.github.ajalt.clikt.core.CliktCommand |
|
import com.github.ajalt.clikt.core.subcommands |
|
import com.github.ajalt.clikt.parameters.arguments.argument |
|
import com.github.ajalt.clikt.parameters.options.default |
|
import com.github.ajalt.clikt.parameters.options.option |
|
import com.github.ajalt.clikt.parameters.options.required |
|
import com.github.ajalt.clikt.parameters.types.enum |
|
import com.github.ajalt.clikt.parameters.types.file |
|
import org.joox.JOOX.and |
|
import org.joox.JOOX.attr |
|
import java.io.ByteArrayOutputStream |
|
import java.io.File |
|
import java.io.ObjectInputStream |
|
import java.io.ObjectOutputStream |
|
import java.util.* |
|
import org.joox.JOOX.`$` as el |
|
|
|
enum class ArgType { |
|
BOOLEAN, STRING, INT, DOUBLE |
|
} |
|
|
|
fun matchByName(file: File, connectioName: String, paramName: String) = el(file) |
|
.find("ExtendedConnectionData option") |
|
.filter(and(attr("name", "name"), attr("value", connectioName))) |
|
.parent() |
|
.find("map entry") |
|
.filter(attr("key", paramName)) |
|
|
|
|
|
class Main : CliktCommand() { |
|
override fun run() = Unit |
|
} |
|
|
|
class Read : CliktCommand(printHelpOnEmptyArgs = true) { |
|
val path by option( |
|
"--path", |
|
"-p", |
|
envvar = "BDIDE_XML", |
|
help = "Path to bdide_settings.xml. May be set via BDIDE_XML environment variable." |
|
) |
|
.file( |
|
mustExist = true, |
|
canBeDir = false, |
|
canBeFile = true, |
|
mustBeReadable = true |
|
).required() |
|
|
|
val connectionName by option( |
|
"--connection", |
|
"-c", |
|
envvar = "BDIDE_CONNECTION", |
|
help = "Name of connection to modify. May be set via BDIDE_CONNECTION environment variable" |
|
).required() |
|
|
|
val paramName by option("--param", "-P", help = "Name of parameter to read").required() |
|
|
|
override fun run() { |
|
val data = matchByName(path, connectionName, paramName) |
|
.attr("value") |
|
.let { Base64.getDecoder().decode(it) } |
|
.inputStream() |
|
|
|
println(ObjectInputStream(data).readObject().toString()) |
|
} |
|
} |
|
|
|
class Write : CliktCommand(printHelpOnEmptyArgs = true) { |
|
val type by option( |
|
"--type", |
|
"-t", |
|
help = "Type of data to insert into XML. STRING by default." |
|
) |
|
.enum<ArgType>() |
|
.default(STRING) |
|
|
|
val value by argument(help = "Text representation of the value to be inseterted in settings") |
|
|
|
val path by option( |
|
"--path", |
|
"-p", |
|
envvar = "BDIDE_XML", |
|
help = "Path to bdide_settings.xml. May be set via BDIDE_XML environment variable." |
|
) |
|
.file( |
|
mustExist = true, |
|
canBeDir = false, |
|
canBeFile = true, |
|
mustBeReadable = true, |
|
mustBeWritable = true, |
|
) |
|
.required() |
|
|
|
val connectionName by option( |
|
"--connection", |
|
"-c", |
|
envvar = "BDIDE_CONNECTION", |
|
help = "Name of connection to modify. May be set via BDIDE_CONNECTION environment variable" |
|
) |
|
.required() |
|
|
|
val paramName by option( |
|
"--param", |
|
"-P", |
|
help = "Name of parameter to read" |
|
) |
|
.required() |
|
|
|
override fun run() { |
|
val toEncode = when (type) { |
|
STRING -> value |
|
INT -> value.toInt() |
|
BOOLEAN -> value.toBoolean() |
|
DOUBLE -> value.toDouble() |
|
} |
|
val out = ByteArrayOutputStream() |
|
ObjectOutputStream(out).writeObject(toEncode) |
|
val data = Base64.getEncoder().encode(out.toByteArray()).decodeToString() |
|
matchByName(path, connectionName, paramName).attr("value", data) |
|
println("Done!") |
|
} |
|
|
|
|
|
} |
|
|
|
Main().subcommands(Read(), Write()).main(args) |