Skip to content

Commit

Permalink
[add] support for parsing dockerfile raw content/read file (#10)
Browse files Browse the repository at this point in the history
[add] [add] support for parsing dockerfile raw content/read file
  • Loading branch information
sescobb27 authored and bryanhuntesl committed Oct 8, 2018
1 parent 4029782 commit 2c418bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/ex_docker_build/dockerfile_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ defmodule ExDockerBuild.DockerfileParser do
@continuation ~r/^.*\\\s*$/
@instruction ~r/^\s*(\w+)\s+(.*)$/

@spec parse!(Path.t()) :: list(String.t()) | no_return()
def parse!(path) do
@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

{parsed_lines, _} =
File.read!(path)
content
|> String.split("\n")
|> Enum.reduce({[], false}, fn line, {acc, continuation?} ->
case parse_line(line, continuation?) do
Expand Down
18 changes: 18 additions & 0 deletions test/dockerfile_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,22 @@ defmodule ExDockerBuild.DockerfileParserTest do
"set -xe && REBAR3_DOWNLOAD_URL=\"https://github.com/erlang/rebar3/archive/${REBAR3_VERSION}.tar.gz\" && REBAR3_DOWNLOAD_SHA256=\"40b3c85440f3235c7b149578d0211bdf57d1c66390f888bb771704f8abc71033\" && mkdir -p /usr/src/rebar3-src && curl -fSL -o rebar3-src.tar.gz \"$REBAR3_DOWNLOAD_URL\" && echo \"$REBAR3_DOWNLOAD_SHA256 rebar3-src.tar.gz\" | sha256sum -c - && tar -xzf rebar3-src.tar.gz -C /usr/src/rebar3-src --strip-components=1 && rm rebar3-src.tar.gz && cd /usr/src/rebar3-src && HOME=$PWD ./bootstrap && install -v ./rebar3 /usr/local/bin/ && rm -rf /usr/src/rebar3-src"}
]
end

test "parses file content instead of a file" do
content = """
FROM elixir:1.7.3
VOLUME /Users/kiro/test:/data
RUN echo "hello-world!!!!" > /data/myfile.txt
CMD ["cat", "/data/myfile.txt"]
"""

result = Parser.parse!(content)

assert result == [
{"FROM", "elixir:1.7.3"},
{"VOLUME", "/Users/kiro/test:/data"},
{"RUN", "echo \"hello-world!!!!\" > /data/myfile.txt"},
{"CMD", "[\"cat\", \"/data/myfile.txt\"]"}
]
end
end

0 comments on commit 2c418bb

Please sign in to comment.