aboutsummaryrefslogtreecommitdiff
path: root/client/src/main/scala/Main.scala
blob: 4b11fe0f432af90407658710cff15c0a40aa09b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)

}