Skip to content

Commit

Permalink
Merge pull request #17 from ChristopherDavenport/fixEnvironment
Browse files Browse the repository at this point in the history
Fix Environmental Encoding
  • Loading branch information
ChristopherDavenport authored Jul 21, 2020
2 parents 32e9386 + 7fd595f commit 7e1c2df
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions core/src/main/scala/io/chrisdavenport/whaletail/Containers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,43 @@ object Containers {

private val containersPrefix = Docker.versionPrefix / "containers"

def create[F[_]: JsonDecoder: Bracket[*[_], Throwable]](
def create[F[_]: Sync](
client: Client[F],
image: String,
exposedPorts: Map[Int, Int] = Map.empty, // Container Port, Host Port
env: Map[String, String] = Map.empty
) =
client.run(
Request[F](Method.POST, containersPrefix / "create")
.withEntity(Json.obj(
"Image" -> image.asJson,
"ExposedPorts" -> Json.obj(
exposedPorts.toList.map{ case (i, _) => s"$i/tcp" -> Json.obj()}:_*
),
"Env" -> Alternative[Option].guard(env.size > 0).as(
List(
env.toList.map{case (key, value) => s"$key=$value"}
).asJson
).asJson,
"HostConfig" -> Json.obj(
"PortBindings" -> Json.obj(
exposedPorts.toList.map{ case (container, host) => s"$container/tcp" -> Json.arr(
Json.obj(
"HostPort" -> s"$host".asJson
)
)}:_*
.withEntity{
Json.obj(
"Image" -> image.asJson,
"ExposedPorts" -> Json.obj(
exposedPorts.toList.map{ case (i, _) => s"$i/tcp" -> Json.obj()}:_*
),
"Env" -> Alternative[Option].guard(env.size > 0).as(
env.toList.map{case (key, value) => s"$key=$value"}.asJson
).asJson,
"HostConfig" -> Json.obj(
"PortBindings" -> Json.obj(
exposedPorts.toList.map{ case (container, host) => s"$container/tcp" -> Json.arr(
Json.obj(
"HostPort" -> s"$host".asJson
)
)}:_*
)
)
)
).dropNullValues
)
).dropNullValues
}
).use{resp =>
JsonDecoder[F].asJsonDecode[Data.ContainerCreated](resp)
if (resp.status === Status.Created)
JsonDecoder[F].asJsonDecode[Data.ContainerCreated](resp)
else
resp.bodyAsText.compile.string.flatMap{body =>
ApplicativeError[F, Throwable].raiseError[Data.ContainerCreated](
Data.ContainersErrorResponse(resp.status, resp.headers, resp.httpVersion, body)
)
}
}

def inspect[F[_]: JsonDecoder: Bracket[*[_], Throwable]](
Expand Down Expand Up @@ -111,5 +117,8 @@ object Containers {
).mapN(ContainerCreated.apply)
}
}

final case class ContainersErrorResponse(status: Status, headers: Headers, httpVersion: HttpVersion, body: String)
extends Throwable(show"Containers Response Not Expected - Status: $status, headers: $headers, httpVersion: $httpVersion, body:$body")
}
}

0 comments on commit 7e1c2df

Please sign in to comment.