Skip to content

Commit a32f304

Browse files
authored
Merge pull request #11 from sescobb27/support-for-dockerfiles-content
[refactor] split parse!/1 in 2 different function to parse raw content and a file
2 parents 2c418bb + 4f31687 commit a32f304

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Start a mix session with `iex -S mix` and type the following instructions
4444
path = Path.expand("~/workspace/elixir-docker-guide")
4545

4646
{:ok, image_id} = Path.join([path, "Dockerfile"]) |>
47-
ExDockerBuild.DockerfileParser.parse!() |>
47+
ExDockerBuild.DockerfileParser.parse_file!() |>
4848
ExDockerBuild.DockerBuild.build(path)
4949
```
5050

@@ -68,7 +68,7 @@ $> mkdir ~/test
6868
path = Path.expand("./test/fixtures")
6969

7070
{:ok, image_id} = Path.join([path, "Dockerfile_bind.dockerfile"]) |>
71-
ExDockerBuild.DockerfileParser.parse!() |>
71+
ExDockerBuild.DockerfileParser.parse_file!() |>
7272
ExDockerBuild.DockerBuild.build(path)
7373
```
7474

lib/ex_docker_build/dockerfile_parser.ex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ defmodule ExDockerBuild.DockerfileParser do
33
@continuation ~r/^.*\\\s*$/
44
@instruction ~r/^\s*(\w+)\s+(.*)$/
55

6-
@spec parse!(Path.t() | String.t()) :: list(String.t()) | no_return()
7-
def parse!(path_or_content) do
8-
content =
9-
if File.exists?(path_or_content) do
10-
File.read!(path_or_content)
11-
else
12-
path_or_content
13-
end
6+
@spec parse_file!(Path.t()) :: list(String.t()) | no_return()
7+
def parse_file!(path) do
8+
path
9+
|> File.read!()
10+
|> do_parse()
11+
end
12+
13+
@spec parse_content!(String.t()) :: list(String.t()) | no_return()
14+
def parse_content!(content), do: do_parse(content)
1415

16+
@spec do_parse(String.t()) :: list(String.t())
17+
defp do_parse(content) do
1518
{parsed_lines, _} =
1619
content
1720
|> String.split("\n")

test/dockerfile_parser_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule ExDockerBuild.DockerfileParserTest do
1010

1111
def parse(base_dir, file) do
1212
Path.join([base_dir, file])
13-
|> Parser.parse!()
13+
|> Parser.parse_file!()
1414
end
1515

1616
test "parses correctly a simple Dockerfile", %{base_dir: base_dir} do
@@ -67,7 +67,7 @@ defmodule ExDockerBuild.DockerfileParserTest do
6767
CMD ["cat", "/data/myfile.txt"]
6868
"""
6969

70-
result = Parser.parse!(content)
70+
result = Parser.parse_content!(content)
7171

7272
assert result == [
7373
{"FROM", "elixir:1.7.3"},

0 commit comments

Comments
 (0)