From 8ecae787ff7124b008229d958c579c73649dd9e4 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sun, 6 May 2018 13:56:16 -0700 Subject: Initial commit --- client/src/main/scala/Main.scala | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 client/src/main/scala/Main.scala (limited to 'client') diff --git a/client/src/main/scala/Main.scala b/client/src/main/scala/Main.scala new file mode 100644 index 0000000..4b11fe0 --- /dev/null +++ b/client/src/main/scala/Main.scala @@ -0,0 +1,62 @@ +package triad + +import ApiProtocol._ +import http.Request +import commando._ +import spray.json._ + +import scala.concurrent._ +import scala.concurrent.duration._ +import scala.util.control.NonFatal + +object Main { + + val command: Command = cmd("triad")( + opt("server", 's', "url" -> true), + opt("verbose", 'v') + ).sub( + cmd("message")( + opt("author", param = "name" -> true), + pos("content") + ).run { args => + val server = + args.get("server").map(_.head).getOrElse("http://localhost:9090") + val author = args.get("author").map(_.head).getOrElse(sys.env("USER")) + val content = args("content").head + val verbose = args.get("verbose").map(_ => true).getOrElse(false) + + val message = Message(content, author).toJson.compactPrint + + val req = Request("POST", + s"$server/messages", + Map("Content-type" -> "application/json"), + message.getBytes("utf-8")) + + if (verbose) { + System.err.println(req.url) + System.err.println(message) + } + + try { + Await.result(http.send(req), 10.seconds) match { + case resp if 200 <= resp.statusCode && resp.statusCode <= 300 => + sys.exit(0) + case resp => + System.err.println( + s"Bad response code while posting message ${resp.statusCode}.") + sys.exit(1) + } + } catch { + case NonFatal(e) => + System.err.println(e.getMessage) + sys.exit(1) + } + }, + cmd("completion")().run { _ => + System.out.println(command.completion) + } + ) + + def main(args: Array[String]): Unit = commando.parse(args, command) + +} -- cgit v1.2.3