Skip to content

Instantly share code, notes, and snippets.

@asm0dey
Last active February 17, 2021 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asm0dey/9a0e00ba3c779ef03de46c001c3b5bce to your computer and use it in GitHub Desktop.
Save asm0dey/9a0e00ba3c779ef03de46c001c3b5bce to your computer and use it in GitHub Desktop.
To run this script you should have kotlinc in your PATH

Script name should end with .main.kts

This script may be run 2 ways:

  1. kotinc -script -jvm-target 1.8 modifier.main.kts -- <arguments here>
  2. If script has executable bit (chmod +x) it may be run with ./modifier.main.kts -- <arguments here> as it has proper shebang.

This script will update (or read) only values, which are stored inside <map> tag (because only they are sored in our specific format)

#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment