diff --git a/README.md b/README.md index 1e496f1..32f5345 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Start a mix session with `iex -S mix` and type the following instructions path = Path.expand("~/workspace/elixir-docker-guide") {:ok, image_id} = Path.join([path, "Dockerfile"]) |> - ExDockerBuild.DockerfileParser.parse!() |> + ExDockerBuild.DockerfileParser.parse_file!() |> ExDockerBuild.DockerBuild.build(path) ``` @@ -68,7 +68,7 @@ $> mkdir ~/test path = Path.expand("./test/fixtures") {:ok, image_id} = Path.join([path, "Dockerfile_bind.dockerfile"]) |> - ExDockerBuild.DockerfileParser.parse!() |> + ExDockerBuild.DockerfileParser.parse_file!() |> ExDockerBuild.DockerBuild.build(path) ``` diff --git a/lib/ex_docker_build/dockerfile_parser.ex b/lib/ex_docker_build/dockerfile_parser.ex index 404998b..ad437f3 100644 --- a/lib/ex_docker_build/dockerfile_parser.ex +++ b/lib/ex_docker_build/dockerfile_parser.ex @@ -3,15 +3,18 @@ defmodule ExDockerBuild.DockerfileParser do @continuation ~r/^.*\\\s*$/ @instruction ~r/^\s*(\w+)\s+(.*)$/ - @spec parse!(Path.t() | String.t()) :: list(String.t()) | no_return() - def parse!(path_or_content) do - content = - if File.exists?(path_or_content) do - File.read!(path_or_content) - else - path_or_content - end + @spec parse_file!(Path.t()) :: list(String.t()) | no_return() + def parse_file!(path) do + path + |> File.read!() + |> do_parse() + end + + @spec parse_content!(String.t()) :: list(String.t()) | no_return() + def parse_content!(content), do: do_parse(content) + @spec do_parse(String.t()) :: list(String.t()) + defp do_parse(content) do {parsed_lines, _} = content |> String.split("\n") diff --git a/test/dockerfile_parser_test.exs b/test/dockerfile_parser_test.exs index 393fca5..b5bec2f 100644 --- a/test/dockerfile_parser_test.exs +++ b/test/dockerfile_parser_test.exs @@ -10,7 +10,7 @@ defmodule ExDockerBuild.DockerfileParserTest do def parse(base_dir, file) do Path.join([base_dir, file]) - |> Parser.parse!() + |> Parser.parse_file!() end test "parses correctly a simple Dockerfile", %{base_dir: base_dir} do @@ -67,7 +67,7 @@ defmodule ExDockerBuild.DockerfileParserTest do CMD ["cat", "/data/myfile.txt"] """ - result = Parser.parse!(content) + result = Parser.parse_content!(content) assert result == [ {"FROM", "elixir:1.7.3"},