diff --git a/lib/testcontainers_elixir.ex b/lib/testcontainers_elixir.ex index 304e549..e61b7c5 100644 --- a/lib/testcontainers_elixir.ex +++ b/lib/testcontainers_elixir.ex @@ -5,6 +5,8 @@ defmodule TestcontainersElixir do """ def hello do + :hello.parse_docker_host(System.get_env("DOCKER_HOST", "")) + |> IO.inspect() :hello.fetch_hello("world") end end diff --git a/src/hello.gleam b/src/hello.gleam index 8af3984..c01176d 100644 --- a/src/hello.gleam +++ b/src/hello.gleam @@ -1,5 +1,8 @@ +// SPDX-License-Identifier: Apache-2.0 import gleam/hackney import gleam/http/request +import gleam/uri +import gleam/option.{None, Option, Some, unwrap} pub fn fetch_hello(name: String) -> String { let assert Ok(request) = @@ -12,3 +15,39 @@ pub fn fetch_hello(name: String) -> String { response.body } + +pub type DockerHost { + DockerHost( + scheme: String, + socket_host: Option(String), + socket_port: Option(Int), + host: Option(String), + port: Option(Int), + ) +} + +// a function to parse DOCKER_HOST env var value +// will crash if provided with nil String for docker_host +pub fn parse_docker_host(docker_host: String) -> Option(DockerHost) { + let assert Ok(uri) = uri.parse(docker_host) + let assert scheme = unwrap(uri.scheme, "no scheme") + case scheme { + "tcp" | "http" -> + Some(DockerHost( + scheme: scheme, + socket_host: None, + socket_port: None, + host: uri.host, + port: uri.port, + )) + "unix" | "npipe" -> + Some(DockerHost( + scheme: scheme, + socket_host: Some("localhost"), + socket_port: Some(2375), + host: uri.host, + port: uri.port, + )) + _ -> None + } +}