main.com.enniovisco.cli.Cli.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webmonitor Show documentation
Show all versions of webmonitor Show documentation
A formal approach to monitoring web pages as spatio-temporal traces.
The newest version!
package com.enniovisco.cli
import com.enniovisco.reporting.*
import javax.script.*
class Cli(
args: Array,
toConsole: Boolean = false,
toFile: Boolean = false,
preloaded: Boolean = false,
exec: Cli.(Reporter) -> Unit
) {
private val report = Reporter(toConsole, toFile)
private val log = io.github.oshai.kotlinlogging.KotlinLogging.logger {}
init {
report.use {
if(!preloaded) {
validateArgs(args)
}
exec(it)
}
}
private fun validateArgs(args: Array) {
try {
when (args.size) {
0 -> defaultScript()
1 -> oneArgument(args)
2 -> twoArguments(args)
else -> log.info { "Using custom scripts" }
}
} catch (e: NullPointerException) {
if (args.size > 1)
log.error { errorMsg(source = args[0], spec = args[1]) }
else
log.error { errorMsg(source = args[0], spec = args[0]) }
}
}
private fun defaultScript() {
log.info { "No arguments provided, using default scripts" }
loadScripts(source = "sample", spec = "sample")
}
private fun oneArgument(args: Array) {
val (source) = args
log.info {
"Using 'source.$source.kts' as source script and " +
"'spec.$source.kts' as spec script"
}
loadScripts(source = source, spec = source)
}
private fun twoArguments(args: Array) {
val (source, spec) = args
log.info {
"Using 'source.$source.kts' as source script and " +
"'spec.$spec.kts' as spec script"
}
loadScripts(source = source, spec = spec)
}
private fun errorMsg(source: String, spec: String) =
"Cannot find any files named 'source.$source.kts' and" +
" 'spec.$spec.kts'. Please make sure that the files exist " +
"and are in the 'resources' directory."
private fun loadScripts(source: String, spec: String) {
with(ScriptEngineManager().getEngineByExtension("kts")) {
eval(loadResource("source.$source.kts"))
eval(loadResource("spec.$spec.kts"))
}
}
private fun loadResource(name: String) =
object {}.javaClass.classLoader.getResourceAsStream(name)
?.bufferedReader()
}