aboutsummaryrefslogtreecommitdiff
path: root/client/src/Main.scala
diff options
context:
space:
mode:
authorJakob Odersky <jakob@inpher.io>2019-10-09 17:10:43 -0400
committerJakob Odersky <jakob@inpher.io>2019-10-09 20:33:16 -0400
commit0ceee5ed4bae240b8c8e94d2fd7424d9d0b67ec7 (patch)
tree2df0258f81050e6fed51d38e217c4f6256518e12 /client/src/Main.scala
parentfaed28c54900fc0b359700873367095f51425794 (diff)
downloadscala-triad-0ceee5ed4bae240b8c8e94d2fd7424d9d0b67ec7.tar.gz
scala-triad-0ceee5ed4bae240b8c8e94d2fd7424d9d0b67ec7.tar.bz2
scala-triad-0ceee5ed4bae240b8c8e94d2fd7424d9d0b67ec7.zip
Migrate build to mill
Diffstat (limited to 'client/src/Main.scala')
-rw-r--r--client/src/Main.scala62
1 files changed, 62 insertions, 0 deletions
diff --git a/client/src/Main.scala b/client/src/Main.scala
new file mode 100644
index 0000000..4b11fe0
--- /dev/null
+++ b/client/src/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)
+
+}