summaryrefslogtreecommitdiff
path: root/crashboxd/src/main/scala/io/crashbox/ci/Parser.scala
diff options
context:
space:
mode:
Diffstat (limited to 'crashboxd/src/main/scala/io/crashbox/ci/Parser.scala')
-rw-r--r--crashboxd/src/main/scala/io/crashbox/ci/Parser.scala44
1 files changed, 44 insertions, 0 deletions
diff --git a/crashboxd/src/main/scala/io/crashbox/ci/Parser.scala b/crashboxd/src/main/scala/io/crashbox/ci/Parser.scala
new file mode 100644
index 0000000..5a58612
--- /dev/null
+++ b/crashboxd/src/main/scala/io/crashbox/ci/Parser.scala
@@ -0,0 +1,44 @@
+package io.crashbox.ci
+
+import io.crashbox.ci.yaml._
+import io.crashbox.ci.yaml.DefaultReaders._
+
+object Parser {
+ sealed trait Result
+ case class Success(buildDef: BuildDef) extends Result
+ case class Failure(error: String) extends Result
+
+ implicit object TaskDefReader extends YamlReader[TaskDef] {
+ def read(value: YamlValue) = {
+ val items = value.convertTo[Map[String, YamlValue]]
+ val image = items
+ .getOrElse("image",
+ throw new YamlFormatException("no image specified"))
+ .convertTo[String]
+ val script = items
+ .getOrElse("script",
+ throw new YamlFormatException("no script specified"))
+ .convertTo[String]
+ TaskDef(DockerEnvironment(image), script)
+ }
+ }
+
+ implicit object BuildDefReader extends YamlReader[BuildDef] {
+ def read(value: YamlValue) = {
+ val items = value.convertTo[Map[String, YamlValue]]
+ val tasks = items
+ .getOrElse("tasks",
+ throw new YamlFormatException("no tasks specified"))
+ .convertTo[Map[String, TaskDef]]
+ BuildDef(tasks.values.toSeq)
+ }
+ }
+
+ def parse(build: String): Result =
+ try {
+ Success(Yaml.parse(build).convertTo[BuildDef])
+ } catch {
+ case ex: YamlFormatException => Failure(ex.toString)
+ }
+
+}