CORDA-1764: Make blobinspector use picocli base class (#3940)

* Make blobinspector use picocli base class

* Updated Documentation

* Update and harmonise documentation

* Give the table a bit more padding to help with merge conflicts

* Correct capitalisation of short options

* Some grammar changes
This commit is contained in:
Anthony Keenan
2018-09-14 16:37:58 +01:00
committed by GitHub
parent 356bddb2d4
commit 99dd2c5936
9 changed files with 176 additions and 105 deletions

View File

@ -4,6 +4,9 @@ import com.fasterxml.jackson.core.JsonFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.jcabi.manifests.Manifests
import net.corda.client.jackson.JacksonSupport
import net.corda.cliutils.CordaCliWrapper
import net.corda.cliutils.ExitCodes
import net.corda.cliutils.start
import net.corda.core.internal.isRegularFile
import net.corda.core.internal.rootMessage
import net.corda.core.serialization.SerializationContext
@ -21,39 +24,23 @@ import net.corda.serialization.internal.SerializationFactoryImpl
import net.corda.serialization.internal.amqp.AbstractAMQPSerializationScheme
import net.corda.serialization.internal.amqp.DeserializationInput
import net.corda.serialization.internal.amqp.amqpMagic
import org.slf4j.event.Level
import picocli.CommandLine
import picocli.CommandLine.*
import java.io.PrintStream
import java.net.MalformedURLException
import java.net.URL
import java.nio.file.Paths
import java.util.*
import kotlin.system.exitProcess
fun main(args: Array<String>) {
val main = BlobInspector()
try {
CommandLine.run(main, *args)
} catch (e: ExecutionException) {
val throwable = e.cause ?: e
if (main.verbose) {
throwable.printStackTrace()
} else {
System.err.println("*ERROR*: ${throwable.rootMessage}. Use --verbose for more details")
}
exitProcess(1)
}
BlobInspector().start(args)
}
@Command(
name = "blob-inspector",
versionProvider = CordaVersionProvider::class,
mixinStandardHelpOptions = true, // add --help and --version options,
showDefaultValues = true,
description = ["Convert AMQP serialised binary blobs to text"]
)
class BlobInspector : Runnable {
@Parameters(index = "0", paramLabel = "SOURCE", description = ["URL or file path to the blob"], converter = [SourceConverter::class])
var source: URL? = null
class BlobInspector : CordaCliWrapper("blob-inspector", "Convert AMQP serialised binary blobs to text") {
@Parameters(index = "*..0", paramLabel = "SOURCE", description = ["URL or file path to the blob"], converter = [SourceConverter::class])
var source: MutableList<URL> = mutableListOf()
@Option(names = ["--format"], paramLabel = "type", description = ["Output format. Possible values: [YAML, JSON]"])
private var formatType: OutputFormatType = OutputFormatType.YAML
@ -68,17 +55,19 @@ class BlobInspector : Runnable {
@Option(names = ["--schema"], description = ["Print the blob's schema first"])
private var schema: Boolean = false
@Option(names = ["--verbose"], description = ["Enable verbose output"])
var verbose: Boolean = false
override fun runProgram() = run(System.out)
override fun run() = run(System.out)
fun run(out: PrintStream) {
override fun initLogging() {
if (verbose) {
System.setProperty("logLevel", "trace")
loggingLevel = Level.TRACE
}
val loggingLevel = loggingLevel.name.toLowerCase(Locale.ENGLISH)
System.setProperty("logLevel", loggingLevel) // This property is referenced from the XML config file.
}
val inputBytes = source!!.readBytes()
fun run(out: PrintStream): Int {
require(source.count() == 1) { "You must specify URL or file path to the blob" }
val inputBytes = source.first().readBytes()
val bytes = parseToBinaryRelaxed(inputFormatType, inputBytes)
?: throw IllegalArgumentException("Error: this input does not appear to be encoded in Corda's AMQP extended format, sorry.")
@ -102,6 +91,9 @@ class BlobInspector : Runnable {
val deserialized = bytes.deserialize<Any>(context = SerializationDefaults.STORAGE_CONTEXT)
out.println(deserialized.javaClass.name)
mapper.writeValue(out, deserialized)
return ExitCodes.SUCCESS
} catch(e: Exception) {
return ExitCodes.FAILURE
} finally {
_contextSerializationEnv.set(null)
}

View File

@ -53,7 +53,7 @@ class BlobInspectorTest {
}
private fun run(resourceName: String): String {
blobInspector.source = javaClass.getResource(resourceName)
blobInspector.source = mutableListOf(javaClass.getResource(resourceName))
val writer = StringWriter()
blobInspector.run(PrintStream(WriterOutputStream(writer, UTF_8)))
val output = writer.toString()