Skip to content

Commit

Permalink
Merge pull request #11 from sescobb27/support-for-dockerfiles-content
Browse files Browse the repository at this point in the history
[refactor] split parse!/1 in 2 different function to parse raw content and a file
  • Loading branch information
sescobb27 authored Oct 8, 2018
2 parents 2c418bb + 4f31687 commit a32f304
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand All @@ -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)
```

Expand Down
19 changes: 11 additions & 8 deletions lib/ex_docker_build/dockerfile_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions test/dockerfile_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down

0 comments on commit a32f304

Please sign in to comment.