From 09a8ddde19736c2024f18848a47d4f6e587bf88b Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Mon, 2 Sep 2019 00:33:11 +0100 Subject: [PATCH] added cli utility --- build.sbt | 20 ++- .../scala/net/woggioni/worth/cli/Main.scala | 126 ++++++++++++++++++ 2 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 cli/src/main/scala/net/woggioni/worth/cli/Main.scala diff --git a/build.sbt b/build.sbt index 13e6a15..24305a8 100644 --- a/build.sbt +++ b/build.sbt @@ -18,7 +18,7 @@ git.useGitDescribe := true fork := true //javaOptions in Test += "-Xmx14G" //scalafmtOnCompile := true -libraryDependencies += "org.projectlombok" % "lombok" % "1.18.8" +libraryDependencies += "org.projectlombok" % "lombok" % "1.18.8" % Provided val testDependencies = Seq("com.novocode" % "junit-interface" % "0.11" % Test, "com.fasterxml.jackson.core" % "jackson-databind" % "2.9.6" % Test, @@ -36,8 +36,20 @@ lazy val worthAntlr = (project in file("antlr")).settings( antlr4PackageName in Antlr4 := Some("net.woggioni.worth.antlr"), skip in publish := true, unmanagedClasspath in Test += (classDirectory in (LocalRootProject, Test)).value, - libraryDependencies += "org.antlr" % "antlr4" % antlrVersion % Compile, - libraryDependencies += "org.antlr" % "antlr4-runtime" % antlrVersion, - libraryDependencies += "org.projectlombok" % "lombok" % "1.18.8", + libraryDependencies += "org.antlr" % "antlr4" % antlrVersion % Test, + libraryDependencies += "org.antlr" % "antlr4-runtime" % antlrVersion % Test, + libraryDependencies += "org.projectlombok" % "lombok" % "1.18.8" % Provided, libraryDependencies ++= testDependencies ).dependsOn(LocalRootProject).enablePlugins(Antlr4Plugin) + +lazy val cli = (project in file("cli")).settings( + organization := (organization in LocalRootProject).value, + name := "worth-cli", + version := (version in LocalRootProject).value, + resourceDirectory := (resourceDirectory in(LocalRootProject, Test)).value, + skip in publish := true, + mainClass := Some("net.woggioni.worth.cli.Main"), + maintainer := "oggioni.walter@gmail.com", + unmanagedClasspath in Test += (classDirectory in (LocalRootProject, Test)).value, + libraryDependencies += "com.beust" % "jcommander" % "1.72" +).dependsOn(LocalRootProject).enablePlugins(JavaAppPackaging).enablePlugins(UniversalPlugin) \ No newline at end of file diff --git a/cli/src/main/scala/net/woggioni/worth/cli/Main.scala b/cli/src/main/scala/net/woggioni/worth/cli/Main.scala new file mode 100644 index 0000000..4e8dda3 --- /dev/null +++ b/cli/src/main/scala/net/woggioni/worth/cli/Main.scala @@ -0,0 +1,126 @@ +package net.woggioni.worth.cli + +import java.io._ + +import com.beust.jcommander.{IStringConverter, JCommander, Parameter, ParameterException} +import net.woggioni.worth.serialization.binary.{JBONDumper, JBONParser} +import net.woggioni.worth.serialization.json.{JSONDumper, JSONParser} +import net.woggioni.worth.xface.Value + +sealed abstract class SerializationFormat(val name : String) { + override def toString = name +} + +object SerializationFormat { + case object JSON extends SerializationFormat("json") + case object JBON extends SerializationFormat("jbon") + + def parse(value : String) : SerializationFormat = { + value match { + case JBON.name => JBON + case JSON.name => JSON + case _ => { + val arr = Stream( + SerializationFormat.JSON, + SerializationFormat.JBON + ).map(_.name).toArray + val availableValues = String.join(", ", arr :_*) + throw new IllegalArgumentException( + s"Unknown serialization format '$value', possible values are $availableValues") + } + } + } +} + +class OutputTypeConverter extends IStringConverter[SerializationFormat] { + override def convert(value: String): SerializationFormat = SerializationFormat.parse(value) +} + +class CliArg { + + @Parameter(names = Array("-f", "--file"), description = "Name of the .cz file to parse") + var fileName : String = _ + + @Parameter(names = Array("--input-type"), description = "Input type", converter = classOf[OutputTypeConverter]) + var inputType : SerializationFormat = SerializationFormat.JSON + + @Parameter(names = Array("-o", "--output"), description = "Name of the JSON file to generate") + var outPut : String = _ + + @Parameter(names = Array("-t", "--type"), description = "Output type", converter = classOf[OutputTypeConverter]) + var outputType : SerializationFormat = SerializationFormat.JSON + + @Parameter(names = Array("-h", "--help"), help = true) + var help : Boolean = _ +} + + +object Main { + + def main(argv : Array[String]): Unit = { + val cliArg = new CliArg + val cliArgumentParser = JCommander.newBuilder() + .addObject(cliArg) + .build() + try { + cliArgumentParser.parse(argv :_*) + } catch { + case _ : ParameterException => { + cliArgumentParser.usage() + System.exit(-1) + } + case e : Throwable => throw e + } + if(cliArg.help) { + cliArgumentParser.usage() + System.exit(0) + } + val cfg = Value.Configuration.builder().serializeReferences(true).build() + val inputStream = if(cliArg.fileName != null) { + new BufferedInputStream(new FileInputStream(cliArg.fileName)) + } else { + System.in + } + + val result = cliArg.inputType match { + case SerializationFormat.JSON => { + val reader = new InputStreamReader(inputStream) + try { + new JSONParser(cfg).parse(reader) + } finally { + reader.close() + } + } + case SerializationFormat.JBON => { + try { + new JBONParser(cfg).parse(inputStream) + } finally { + inputStream.close() + } + } + } + + val outputStream = if(cliArg.outPut != null) { + new BufferedOutputStream(new FileOutputStream(cliArg.outPut)) + } else { + System.out + } + cliArg.outputType match { + case SerializationFormat.JSON => { + val writer = new OutputStreamWriter(outputStream) + try { + new JSONDumper(cfg).dump(result, writer) + } finally { + writer.close() + } + } + case SerializationFormat.JBON => { + try { + new JBONDumper(cfg).dump(result, outputStream) + } finally { + outputStream.close() + } + } + } + } +}