summaryrefslogtreecommitdiff
path: root/crashboxd/src/main/scala/io/crashbox/ci/Parser.scala
blob: 5a586121bedec3e2ac50b28f9b5eafe01718f4a2 (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
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)
    }

}